2009-11-04 49 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()