分隔并打印所有包含三个字母的单词



我需要一些程序的帮助,该程序将从RichTextBox中提取所有具有三个字母的单词并写出这些单词的总和?

我试过这个:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim strInput As String
    strInput = RichTextBox1.Text
    Dim strSplit() As String
    strSplit = strInput.Split(CChar(" "))
    MsgBox("Number of words: " & strSplit.Length)
End Sub

然而,这只是计数,我不知道如何设置一个条件来只计算有三个字母的单词。

使用 LINQ 计算长度为 3 的数组项。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim strInput As String

    strInput = RichTextBox1.Text
    Dim strSplit() As String
    strSplit = strInput.Split(CChar(" "))
    Dim count = From x In strSplit Where x.Length = 3
    Dim sum = (From x In count Select x.Length).Sum()
    MsgBox("Number of words: " & count.Count.ToString())
    MsgBox("All the 3 letter words: " & String.Join(" ", count).ToString())
    MsgBox("sum of the 3 letter words: " & sum.ToString())
End Sub

最新更新