views:

232

answers:

1

I am using VB 9.0 to split a text file and then count occurrences of the term <sequence>. Supposing I want also to count occurrences of the same term in a different format, e.g. <sequence and then group them together such that I output the result to a text box, i.e. txtMyTerms.Text=<sequence>+<sequence. How to do it?

My current code is as follows:

    Dim str As String = txtSource.Text
    Dim arr As String() = str.Split(Nothing)
    Dim searchTerm As String = "<sequence>"

    'create query to search for the term <sequence>
    Dim matchQuery = From word In arr Where word.ToLowerInvariant() = searchTerm.ToLowerInvariant() Select word

    ' Count the matches.
    Dim count As Integer = matchQuery.Count()
    txtMyTerms.Text = count.ToString()
A: 

I'd try something like this. Note that string.Compare is more efficient than repeatedly calling ToLowerInvariant().

Dim str As String = txtSource.Text
Dim arr As String() = str.Split(Nothing)
Dim searchTerm1 As String = "<sequence>"
Dim searchTerm2 As String = "<sequence"

'create query to search for the term <sequence>
Dim matchQuery = From word In arr Where word.Compare(searchTerm1, StringComparison.InvariantCultureIgnoreCase) == 0 Or word.Compare(searchTerm2, StringComparison.InvariantCultureIgnoreCase) == 0 Select word

' Count the matches.
Dim count As Integer = matchQuery.Count()
txtMyTerms.Text = count.ToString()
Justin Grant