File operation, search a particular word in text file

Posted by Rmohankumar under VB.NET on 11/12/2011 | Points: 10 | Views : 2834 | Status : [Member] | Replies : 1
Search a particular word or text in text file and count that word/text how many times placed in this text file using vb.net




Responses

Posted by: Vanajasethu on: 11/25/2011 [Member] Starter | Points: 25

Up
0
Down
string filename = @"D:\sample.txt";
FileStream fs = new FileStream(filename, FileMode.Open);
StreamReader sr = new StreamReader(fs);
do
{
content = sr.ReadLine();
if (Regex.IsMatch(content, searchstr, RegexOptions.IgnoreCase))
{
while ((i = content.IndexOf(searchstr, i)) >=0)
{
count = count + 1;
i++;
}
i = 0;
}

} while (!sr.EndOfStream == true);

textBox1.Text = Convert.ToString(count);
Count will give you how many occurence of word in a text file.

Rmohankumar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response