Finding a SubString within a String

Discussion in 'ASP .Net' started by Badass Scotsman, Mar 31, 2006.

  1. Hello,

    Using VB and ASP,NET I would like to be able to search a STRING for a
    smaller STRING within, based on the characters which appear before and
    after.

    For example:

    String1 = " That was a tasty burger"

    How can I return the word Tasty, by referencing the word "a" and
    "burger"....

    In pseudo style:

    Find all characters between " a " and " burger" (note the spaces), and store
    as a string

    Result:
    tasty


    Any ideas?

    Regards,

    Gary.
    Badass Scotsman, Mar 31, 2006
    #1
    1. Advertising

  2. Flinky Wisty Pomm, Mar 31, 2006
    #2
    1. Advertising

  3. Gary,

    I agree with Flinky. Regular Expressions are probably the best way to go.
    However here are a few subroutines I wrote a while ago that do it a
    different way if you'd like:

    Really the very first method is the answer to the example you give. I just
    have a few other overloaded methods so that the start and end flags of the
    string(s) being searched for may be varied.

    Public Function ExtractString(ByVal stringToSearch As String, ByVal
    startFlag As String, ByVal endFlag As String) As String
    Try
    Dim mintStartLocation, mintEndLocation As Int32
    Dim mstrFound As String = Nothing
    '---Get the start location
    mintStartLocation = stringToSearch.IndexOf(startFlag)
    '---Check that a start location was found and that it isn't at the end of
    the string
    If (mintStartLocation > -1) And (mintStartLocation < stringToSearch.Length)
    Then
    'Search for the end location
    mintEndLocation = stringToSearch.IndexOf(endFlag, mintStartLocation)
    '---Extract the string found
    If mintEndLocation = -1 Then
    '---If no end location was found then grab everything from the start to the
    end
    mstrFound = stringToSearch.Substring(mintStartLocation)
    Else
    mstrFound = stringToSearch.Substring(mintStartLocation, (mintEndLocation -
    mintStartLocation))
    End If
    End If
    Return mstrFound
    Catch e As Exception
    Throw e
    End Try
    End Function

    Public Function ExtractString(ByVal stringToSearch As String, ByVal
    startIndex As Int32, ByVal startFlag As String, ByVal endFlag As String) As
    String
    Try
    Dim mintStartLocation, mintEndLocation As Int32
    Dim mstrFound As String = Nothing
    '---Get the start location
    mintStartLocation = stringToSearch.IndexOf(startFlag, startIndex)
    '---Check that a start location was found and that it isn't at the end of
    the string
    If (mintStartLocation > -1) And (mintStartLocation < stringToSearch.Length)
    Then
    'Search for the end location
    mintEndLocation = stringToSearch.IndexOf(endFlag, mintStartLocation)
    '---Extract the string found
    If mintEndLocation = -1 Then
    '---If no end location was found then grab everything from the start to the
    end
    mstrFound = stringToSearch.Substring(mintStartLocation)
    Else
    mstrFound = stringToSearch.Substring(mintStartLocation, (mintEndLocation -
    mintStartLocation))
    End If
    End If
    Return mstrFound
    Catch e As Exception
    Throw e
    End Try
    End Function

    Public Function ExtractString(ByVal stringToSearch As String, ByVal
    startFlag As String, ByVal endFlags() As String) As String
    Try
    Dim mintStartLocation, mintEndLocation, mintTempEndLocation As Int32
    Dim mstrFound As String = Nothing
    Dim mblnEndFound As Boolean = False
    '---Get the start location
    mintStartLocation = stringToSearch.IndexOf(startFlag)
    '---Check that a start location was found and that it isn't at the end of
    the string
    If (mintStartLocation > -1) And (mintStartLocation < stringToSearch.Length)
    Then
    '---Set the end location equal to the end of the string to search
    mintEndLocation = stringToSearch.Length
    '---Move through each string in the endflag array and set the end flag
    location to the closest one.
    For Each EndString As String In endFlags
    mintTempEndLocation = stringToSearch.IndexOf(EndString, mintStartLocation)
    If (mintTempEndLocation > -1) And (mintTempEndLocation < mintEndLocation)
    Then
    mintEndLocation = mintTempEndLocation
    End If
    Next
    '---Extract the string found
    mstrFound = stringToSearch.Substring(mintStartLocation, (mintEndLocation -
    mintStartLocation))
    End If
    Return mstrFound
    Catch e As Exception
    Throw e
    End Try
    End Function

    Public Function ExtractString(ByVal stringToSearch As String, ByVal
    startIndex As Int32, ByVal startFlag As String, ByVal endFlags() As String)
    As String
    Try
    Dim mintStartLocation, mintEndLocation, mintTempEndLocation As Int32
    Dim mstrFound As String = Nothing
    Dim mblnEndFound As Boolean = False
    '---Get the start location
    mintStartLocation = stringToSearch.IndexOf(startFlag, startIndex)
    '---Check that a start location was found and that it isn't at the end of
    the string
    If (mintStartLocation > -1) And (mintStartLocation < stringToSearch.Length)
    Then
    '---Set the end location equal to the end of the string to search
    mintEndLocation = stringToSearch.Length
    '---Move through each string in the endflag array and set the end flag
    location to the closest one.
    For Each EndString As String In endFlags
    mintTempEndLocation = stringToSearch.IndexOf(EndString, mintStartLocation)
    If (mintTempEndLocation > -1) And (mintTempEndLocation < mintEndLocation)
    Then
    mintEndLocation = mintTempEndLocation
    End If
    Next
    '---Extract the string found
    mstrFound = stringToSearch.Substring(mintStartLocation, (mintEndLocation -
    mintStartLocation))
    End If
    Return mstrFound
    Catch e As Exception
    Throw e
    End Try
    End Function

    Regards,

    --
    S. Justin Gengo
    Web Developer / Programmer

    Free code library:
    http://www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche


    "Badass Scotsman" <> wrote in message
    news:e0j855$jhr$1$...
    > Hello,
    >
    > Using VB and ASP,NET I would like to be able to search a STRING for a
    > smaller STRING within, based on the characters which appear before and
    > after.
    >
    > For example:
    >
    > String1 = " That was a tasty burger"
    >
    > How can I return the word Tasty, by referencing the word "a" and
    > "burger"....
    >
    > In pseudo style:
    >
    > Find all characters between " a " and " burger" (note the spaces), and
    > store as a string
    >
    > Result:
    > tasty
    >
    >
    > Any ideas?
    >
    > Regards,
    >
    > Gary.
    >
    S. Justin Gengo, Mar 31, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. johndesp
    Replies:
    13
    Views:
    1,577
    Paul Lutus
    Aug 20, 2004
  2. Tarun

    Finding a substring in a binary string

    Tarun, Aug 23, 2005, in forum: C Programming
    Replies:
    6
    Views:
    561
    CBFalconer
    Aug 23, 2005
  3. Replies:
    3
    Views:
    189
    Sherm Pendley
    Aug 3, 2005
  4. PerlFAQ Server
    Replies:
    0
    Views:
    161
    PerlFAQ Server
    Jan 4, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    150
    PerlFAQ Server
    Apr 22, 2011
Loading...

Share This Page