How do I find a string in a text file?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have textBox1 which is the string i want to search in file.txt
Button1
I want textBox2 to show the line of text i am searching when i click button1
 
One way is to read the file one line at a time into a String and use the
String class methods to search. However, if the file is large, this approach
may not be the right one.

I have textBox1 which is the string i want to search in file.txt
Button1
I want textBox2 to show the line of text i am searching when i click button1
 
Hi,
you can open the text file then loop through line by line until you
possibly find an instance of the text you are looking for, like:

StreamReader s = new StreamReader(@"c:\somefile.txt");
string currentLine;
string searchString = "hello";
bool foundText = false;

do
{
currentLine = s.ReadLine();
if(currentLine != null)
{
foundText = currentLine.Contains(searchString);
}
}
while(currentLine != null && !foundText);

if(foundText)
{
//do something
}

If you are searching a lot you probably want to cache the file in memory
rather than reading from a file each time, memory permitting.

Hope that helps
Mark R Dawson
http://www.markdawson.org
 
also make sure you always use a try finally to close the stream when you are
finished with it. I didn't put it in my example, tsk, tsk.
 
Mark said:
also make sure you always use a try finally to close the stream when you are
finished with it. I didn't put it in my example, tsk, tsk.

would it be okay to wrap your example with "using (s) {}", or should you
just Close() it manually? any performance issues (or others) with using?
 
I normally use the using statement whenever I have an object that implements
IDisposable, there is no performance hit as under the hood it compiles to a
try finally with the dispose called in the finally and I think using it makes
the intent of the code clearer.

I did not say to use it in my previous example because I didn't see the
Dispose method in the intellisence (which was a little suprising) for
StreamReader so I didn't say to use it, however running a quick example now
shows it implements IDisposable even if Dispose is not showing up in my
intellisence - wierd.

Mark.
 
Mark said:
I normally use the using statement whenever I have an object that implements
IDisposable, there is no performance hit as under the hood it compiles to a
try finally with the dispose called in the finally and I think using it makes
the intent of the code clearer.

I did not say to use it in my previous example because I didn't see the
Dispose method in the intellisence (which was a little suprising) for
StreamReader so I didn't say to use it, however running a quick example now
shows it implements IDisposable even if Dispose is not showing up in my
intellisence - wierd.

Yeah, all the sample code in the help files for StreamWriter/Reader use
the using statement, so I started doing it too. Just wasn't sure if it
was the best way to dispose of a file stream.
 
using is really the cool way to go. If you don't use the using construct
you may fail to call Dispose explicitly, an exception may interupt the
call to Dispose, or you could even invoke a call on an object that has
been disposed.

in fact using and IDisposable is a form of deterministic cleanup as in
the RAII (Resource Acquisition Is Initialization) pattern!

http://www.geocities.com/jeff_louie/deterministic_destructors.htm

Regards,
Jeff
 
Hello,
This worked,Thank you
One more question
How do I get the entire line that the SearchString is in?
 
mjl1976 said:
This worked,Thank you
One more question
How do I get the entire line that the SearchString is in?

Keep hold of searchString at the point where foundText becomes true.
 
Back
Top