The following code shows how to search a particular word in file. The function SearchWord() accepts two parameters i.e.,
SearchWord(filePath As String, searchString As String). Strategy is that: reading a file line by line and splitting at every space and then checking if that word equals to searchString then count plus plus.
Imports System.IO
Public Sub SearchWord(ByVal filePath As String, ByVal searchString As String)
Dim tr As TextReader = File.OpenText(filePath)
Dim wordFound As Integer = 0
Dim line = tr.ReadLine()
While Not line Is Nothing
Dim str = line.Split(" ")
For i = 0 To str.Length - 1
If (str(i).Equals(searchString)) Then
wordFound += 1
End If
Next
line = tr.ReadLine()
End While
Console.WriteLine(wordFound.ToString())
End Sub
0 comments:
Post a Comment