The following code shows you search a word in file through LINQ. Strategy is that: reading a all file data in one string using ReadToEnd() method and splitting at every space. Now using LINQ query in array and check if searchString equals then selecting all that strings. And in the end Count() method can give you the count of your searched word.
Imports System.Linq
Public Sub SearchWord(ByVal filePath As String, ByVal searchString As String)
Dim tr As TextReader = File.OpenText(filePath)
Dim str = tr.ReadToEnd().Split(" ")
Dim query As IEnumerable(Of String) = _
From word As String In str _
Where word.Equals(searchString) _
Select word
Console.WriteLine(query.Count().ToString())
End Sub
0 comments:
Post a Comment