我需要一些程序的帮助,该程序将从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
然而,这只是在计算,我不知道如何设置一个条件来只计算有三个字母的单词。
答案 0 :(得分:1)
使用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