在另一个字符串

时间:2016-10-05 09:17:00

标签: vb.net

我需要在字符串中找到更改指定单词的位置,我需要它非常具体,所以它不包含没有空格的单词所以如果我在寻找单词'嗨'它只会如果检查'Hi'而不是'HiExample',则返回true。

代码:

Dim userString As String = userInput.Text
    userString = userString.ToLower()

    Dim d As New Dictionary(Of String, Integer)
    Dim wordString = userString.ToLower().Split(" "c)
    Dim iList As New List(Of String)()

    For Each word In wordString
        If d.ContainsKey(word) Then
            d(word) += 1
            iList.Add(word)
        Else
            d.Add(word, 1)
        End If
    Next

    For Each de In d
        For i As Integer = 0 To wordString.Count - 1
            Dim index As Integer = userString.IndexOf(de.Key)
            output.Text &= "Word: " & de.Key & " Occurrence: " & de.Value & " Position: " & GET POSTION OF EACH WORD HERE & Environment.NewLine
        Next
    Next

因为我已经将字符串转换为小写,所以不需要检查大写或小写。

谢谢,

马特

1 个答案:

答案 0 :(得分:0)

你可以试试Regex

For Each de In d
   Dim index As Integer = Regex.Matches(userString, "(?<=^|\b|\s)" & Regex.Escape(de.Key) & "(?=\s|\b|$)")(0).Index
   Console.WriteLine("Word: " & de.Key & " Occurrence: " & de.Value & " Position: " & index)
Next

这可能会让你开始