遍历RichTextBox中的每个单词

时间:2020-09-17 19:33:20

标签: vb.net

我正在尝试遍历文本文档中的每个单词,以使用以下代码将每个单词与名称列表进行比较。

            For Each word As String In TextBox1.Text.Split({" "c}, StringSplitOptions.RemoveEmptyEntries)
            Replace(word, vbCrLf, "")
            word = Trim(TrimPunctuation(word))
            MsgBox(word)
            next

Private Function TrimPunctuation(ByVal value As String) As String
    Dim removeFromStart As Integer = 0
    For i As Integer = 0 To value.Length - 1 Step 1
        If Char.IsPunctuation(value(i)) Then
            removeFromStart += 1
        Else
            Exit For
        End If
    Next
    Dim removeFromEnd As Integer = 0
    For i As Integer = value.Length - 1 To 0 Step -1
        If Char.IsPunctuation(value(i)) Then
            removeFromEnd += 1
        Else
            Exit For
        End If
    Next
    Return Trim(value.Substring(removeFromStart,
                           value.Length - removeFromEnd - removeFromStart))
End Function

在大多数情况下,它是有效的,但是在每个句子的末尾,它返回的最后一个单词包括标点符号和回车以及下一个句子的第一个单词。

晚餐。

然后

1 个答案:

答案 0 :(得分:0)

我创建了一个列表来保存输出。
接下来,我删除了所有辛苦的回报。
该行分为单词。空格字符后的小c告诉编译器这是Char,这是Split函数所期望的。
我创建了一个标点符号的Char数组。您可能需要添加其他字符。
我遍历单词,修剪标点符号。然后将无标点的单词添加到清除列表中。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim CleanList As New List(Of String)
    Dim RemovedNewLine = RichTextBox1.Text.Replace(vbLf, " ")
    Dim words = RemovedNewLine.Split({" "c}, StringSplitOptions.RemoveEmptyEntries)
    Dim punc() As Char = {","c, "."c, "?"c, "!"c}
    For Each word In words
        Dim TrimmedWord = word.Trim(punc)
        CleanList.Add(TrimmedWord)
    Next
    For Each word In CleanList
        Debug.Print(word)
    Next
End Sub