如何用VB.NET计算部分匹配单词的出现?

时间:2009-11-04 11:19:39

标签: regex split linq-query-syntax

我正在使用VB 9.0拆分文本文件,然后计算术语<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()

1 个答案:

答案 0 :(得分: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()