2016-10-05 102 views
0

我需要在字符串中查找指定單詞的位置,我需要它是非常具體的,所以它不包含沒有空格的單詞,所以說如果我是尋找單詞'嗨'它只會返回true,如果它是檢查'嗨'而不是'HiExample'。在另一個字符串中查找指定字符串的位置

代碼:正如我已經轉換的串入下殼體

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 

檢查大寫或小寫將不是必需的。

感謝,

馬特

+0

你瞭解正則表達式?這裏可能有用 – ADyson

回答

0

你可以嘗試正則表達式

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 

,這可能讓你開始

相關問題