Results 1 to 4 of 4

Thread: Parse (find string in) text file?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    top of the mountain
    Posts
    234

    Question Parse (find string in) text file?

    Hi,
    I'm wondering what is faster way to find string in another string(filled from 350kb text file) using string.IndexOf method or using regular expression.

    thanks j

  2. #2
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: Parse (find string in) text file?

    I don't know if its faster, but you can try this:

    VB Code:
    1. Private Function FindIndex(data as String, search as String) as Int32
    2.    Dim found as Boolean = False
    3.    Dim i as Int32
    4.    For i = 0 to data.Length - 1
    5.       If data.SubString(i, search.Length) = search
    6.          found = True
    7.          Exit For
    8.       End If
    9.    Next
    10.  
    11.    If found Then
    12.       Return i
    13.    End If
    14.    Return -1
    15. End Function

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Parse (find string in) text file?

    using IndexOf is faster that using RegEx because the RegEx compiles the pattern before searching
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Parse (find string in) text file?

    Regex is handy to search for patterns of text, not one particular string that you know will come up. Like searching for a phone number. You can use Regex to match on a phone number pattern and have it return all phone numbers in the file that matches the pattern, instead of coding the phone number that you want to search for and using .IndexOf or some other method. If its not a pattern or special kind of string you want to search for, then Regex shouldn't be necessary.

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