-
Jan 7th, 2005, 11:39 AM
#1
Thread Starter
Fanatic Member
search for str in file [resolved]
I am kind of new to vb.net, im an old vb6 pro
Im having issues writing code to search for a string within a file. Mainly, I really dont know much about the new file handling features of vb.net.
Example:
searchString = "red"
Contents of file to search:
<font color="red">
Any help is greatly appreciated.
Last edited by nkad; Jan 7th, 2005 at 03:49 PM.
-
Jan 7th, 2005, 11:56 AM
#2
Re: search for str in file
I dont have a IDE here but...
VB Code:
Imports System.IO ' for StreamReader
'-----
Public Function SearchFile(FileName as String, SearchString as String) as Boolean
Dim sr as new StreamReader(FileName)
Dim strLines() as String = sr.ReadToEnd.Split(vbcrlf)
sr.Close
sr = nothing
Dim intIndex as Integer '
For Each s as String in strLines
intIndex = s.IndexOf(SearchString )' IndexOf returns -1 if the string not found
If not intIndex = -1 Then
Return True
End If
Next
This simple method returns true if the file contains the SearchString (it does not continue looking after it finds a match.
VB Code:
'usage
Msgbox (SearchFile("c:\test.txt", "red"))
Last edited by <ABX; Jan 7th, 2005 at 09:05 PM.
Reason: Post Editor did not insert a new line ???
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
-
Jan 7th, 2005, 12:17 PM
#3
Thread Starter
Fanatic Member
Re: search for str in file
Last edited by nkad; Jan 7th, 2005 at 03:49 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|