问题描述:

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
相关阅读:
Top