Click to See Complete Forum and Search --> : Search String in a text file


ahbeng123
March 2nd, 2006, 03:56 AM
Hi,

Anyone have any idea on how to search a particular string in a text file?
If using StreamReader, can i do that?
I am writting a function to return true,false; if i could find the particular string.
Please advice. Thanks.

e.g, i wan to open sample.txt to look for "nine"? do i need to read line by line?or i have other option? thanks

sample.txt
one,1
two,2
three,3
..............

hansipet
March 2nd, 2006, 03:58 AM
ReadToEnd or ReadLine and then string.contains

Regards
Hansjörg

battula32
March 2nd, 2006, 09:49 AM
class TestFileIO
{
static void Main()
{
string fileName = "test.txt"; // a sample file name

// Delete the file if it exists.
if (System.IO.File.Exists(fileName))
{

string s = "";
System.IO.StreamReader sr = System.IO.File.OpenText(fileName)
while ((s = sr.ReadLine()) != null)
{
if(s.contains("Sample text")
MessageBox.show("Message");

}
}
}
}

just like u can get this in MSDN

http://msdn2.microsoft.com/en-us/library/ms228592.aspx

May be it will help you

regards
Ravi

ahbeng123
March 2nd, 2006, 06:51 PM
thank you so much, i would try it out......