如何使用VB.NET统计部分匹配单词的出现次数?

发布于 2024-08-10 09:50:07 字数 827 浏览 1 评论 0 原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不离久伴 2024-08-17 09:50:07

我会尝试这样的事情。请注意,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()

I would 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文