2009-11-04 59 views
1

我使用VB 9.0来分割文本文件,然后计算术语<sequence>的出现次数。假设我也想用不同的格式来统计同一词的出现次数,例如<sequence然后它们组合在一起,这样我将结果输出到一个文本框,即如何计算与VB.NET的部分匹配词的出现?

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

回答

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