I'm trying to count instances of words in a text box and display the count number and word in a listbox. Here's what I have now:
Private Sub SortUp() Dim source As String = txtData.Text Dim WordList As String = "" Dim result As New System.Text.StringBuilder Dim parser As New Regex("\b[a-z]+\b")
source = source.ToLower
txtWordList.Text =
"" Dim sourceMatches As MatchCollection = parser.Matches(source) Dim counter As Integer 'Dim finder As Integer = sourceMatches(counter).Value.ToString() For counter = 0 To sourceMatches.Count - 1result.Append(sourceMatches(counter).Value.ToString()).Append(
" ") Next counterWordList = WordList & result.ToString.Trim
Dim S() As String = Split(WordList, " ")Array.Sort(S)
Dim Word As String Dim Idx As Single = 0 Dim Qty As Single = 1 Dim tempWord As String = ""txtWordList.ForeColor = Color.Black
'Dim WordCountArray As StringlstData.Items.Clear()
Dim Qty_tmp As Single = 1 For Each Word In S If tempWord = Word ThenQty += 1
lstData.Items.Add(Idx &
" (" & Qty & ") " & Word) 'Debug.Print(Idx & " (" & Qty & ") " & Word) ElseIdx += 1
'check for last instanceQty = 1
'Debug.Print(Idx & " (" & Qty & ") " & Word)lstData.Items.Add(Idx &
" (" & Qty & ") " & Word)tempWord = Word
End If Next End SubI tried a test file like this: "now hear this now this deep now" and got these results:
1 (1) deep
2 (1) hear
3 (1) now
3 (2) now
3 (3) now
4 (1) this
4 (2) this
The result I want is:
1 (1) deep
2 (1) hear
3 (3) now
4 (2) this
Can anyone suggest how to do this