在富文本框中拆分不同的单词

时间:2013-12-06 00:53:02

标签: vb.net

我一直试图在富文本框中显示不同单词的数量。我已经尝试了各种方式,但它仍然是相同的。坚持帮助。谢谢

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim FILE_NAME As String = "C:\Users\User\Desktop\FOR\testit.txt"   'read file from an existing folder in the computer
        If System.IO.File.Exists(FILE_NAME) = True Then 'check if file exists
            Dim fReader As New System.IO.StreamReader(FILE_NAME)
            RichTextBox2.Text = fReader.ReadToEnd 'read the file into the rich text box to the end
            fReader.Close()
        Else : MsgBox("No such file exists") 'if the file doesnt exist, prompt a msg box that no such file exists
        End If

        Dim newWords As String
        newWords = RichTextBox2.Text
        Dim word1 As Array
        word1 = newWords.Split(" ")
        Dim counter = 0
        Dim input As Array = word1
        Dim check As New Dictionary(Of String, String)()
        For Each p As String In input
            If Not check.ContainsKey(p) Then
                check.Add(p, 0)
                counter = counter + 1  'count distinct words in an array

            End If
            Label4.Text = counter 'display number of distinct words in label4
            check(p) += 1

        Next

1 个答案:

答案 0 :(得分:1)

你总是可以这样做:

    Label4.Text =
        RichTextBox2 _
            .Text _
            .Split(" ") _
            .ToLookup(Function(x) x) _
            .Count()