1

我正在使用VB 9.0来拆分文本文件,然后计算 term 的出现次数<sequence>。假设我还想以不同格式计算相同术语的出现次数,例如<sequence,然后将它们组合在一起,以便我将结果输出到文本框,即

txtMyTerms.Text=<sequence>+<sequence

怎么做?我目前的代码如下:

    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()
4

1 回答 1

0

我会尝试这样的事情。请注意,string.Compare 比重复调用 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()
于 2009-11-04T11:58:17.753 回答