Results 1 to 3 of 3

Thread: search for str in file [resolved]

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770

    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.

  2. #2
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Re: search for str in file

    I dont have a IDE here but...

    VB Code:
    1. Imports System.IO ' for StreamReader
    2.  
    3. '-----
    4.  
    5. Public Function SearchFile(FileName as String, SearchString as String) as Boolean
    6.  
    7. Dim sr as new StreamReader(FileName)
    8.  
    9. Dim strLines() as String = sr.ReadToEnd.Split(vbcrlf)
    10.  
    11. sr.Close
    12. sr = nothing
    13.  
    14. Dim intIndex as Integer '
    15.  
    16. For Each s as String in strLines
    17.     intIndex = s.IndexOf(SearchString )' IndexOf returns -1 if the string not found
    18.     If not intIndex = -1 Then
    19.        Return True
    20.     End If
    21. Next

    This simple method returns true if the file contains the SearchString (it does not continue looking after it finds a match.

    VB Code:
    1. 'usage
    2.  
    3. 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

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770

    Re: search for str in file

    thanks.... that helps..
    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
  •  



Click Here to Expand Forum to Full Width