Finding string in text file

  • Thread starter Thread starter Phil
  • Start date Start date
P

Phil

Hi,

Is there a 'shorter' way to find a string within a text file without
'testing' each character or using the LineInput function. Does the
StreamReader have any search facility?

Thanks,

Phil
 
* "Phil said:
Is there a 'shorter' way to find a string within a text file without
'testing' each character or using the LineInput function. Does the
StreamReader have any search facility?

You can read the file line by line and use the line's 'IndexOf' method
to find the string.
 
Addendum:

Ooops... Pressed send keys instead of pasting code:

Basic code for reading the lines of a file:

\\\
Imports System.IO
..
..
..
Dim sr As New StreamReader("C:\WINDOWS\WIN.INI")
Dim strLine As String
strLine = sr.ReadLine()
Do Until strLine Is Nothing
MsgBox(strLine)
strLine = sr.ReadLine()
Loop
sr.Close()
///
 
Phil,
You can use StreamReader.ReadToEnd to read the entire string. Then you can
use String.IndexOf to find the string.

Just be mindful of large files, reading the entire file into memory at once
can put undue pressure on the GC...
Does the
StreamReader have any search facility?
No

Hope this helps
Jay
 
Thanks to Jay and Herfried. I will look into the .IndexOf method.

Have a great week.

Phil
 
Back
Top