当前位置: 动力学知识库 > 问答 > 编程问答 >

vb.net - count occurrence of particular text in txt file

问题描述:

Dose anybody know how I can tally instances of a particular text occurrence within a txt file using VB.net. Better explanation if I use a textbox and type in a text how I can use VB.net to search a txt file for that piece of text and return the amount counted in another textbox or label.

网友答案:

You could also use LINQ to achieve this:

Dim separator = {" "c}
Dim words = (From line As String In IO.File.ReadLines("C:\Temp\SampleFile.txt")
            From word In line.Split(separator, StringSplitOptions.RemoveEmptyEntries)
            Where word = TxtWordToFind.Text).Count

The same in method syntax(crabbed in VB):

words = IO.File.ReadLines("C:\Temp\SampleFile.txt").
        SelectMany(Function(line) line.Split(separator, StringSplitOptions.RemoveEmptyEntries)).
        Where(Function(word) word = TxtWordToFind.Text).Count
分享给朋友:
您可能感兴趣的文章:
随机阅读: