1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

VB.NET - Getting String Between 2 Strings

Discussion in 'Visual Basic .NET' started by gimme4free, Oct 6, 2011.

  1. gimme4free

    gimme4free Executive VIP Jr. VIP Premium Member

    Joined:
    Oct 22, 2008
    Messages:
    1,879
    Likes Received:
    1,932
    I have 2 functions, 1 which I converted from PHP - VB that uses Regex (Requires System.Text.RegularExpressions). The other which was shared by a member on this site.

    The problem that I am facing is that neither of the functions allow you to get the string between 2 escaped characters ("").

    Function 1 (Regex):
    Code:
    Public Function GetStringBetween(ByVal Haystack As String, ByVal StartSearch As String, ByVal EndSearch As String) As String
            Dim rx As New Regex(StartSearch & "(.+?)" & EndSearch)
            Dim m As Match = rx.Match(Haystack)
            If m.Success Then
                Return m.Groups(1).ToString()
            Else
                Return False
            End If
        End Function

    Function 2:
    Code:
    Public Function GetBetween(ByVal haystack As String, ByVal needle As String, ByVal needle_two As String) As String
            Dim istart As Integer = InStr(haystack, needle)
            If istart > 0 Then
                Dim istop As Integer = InStr(istart, haystack, needle_two)
                If istop > 0 Then
                    Try
                        Dim value As String = haystack.Substring(istart + Len(needle) - 1, istop - istart - Len(needle))
                        Return value
                    Catch ex As Exception
                        Return ""
                    End Try
                End If
            End If
            Return ""
        End Function
    Example string:
    Code:
    Dim Haystack As String = "<input type=""hidden"" name=""testsubmit"" id=""testsubmit"" value=""findme""  />"
    Dim FindIt As String = GetBetween(Haystack, "id=""testsubmit"" value=""", """ ")
    MessageBox.Show(FindIt)

    Neither of the above functions will find the 'findme' that it should be able to pull out whereas both functions will work on a simple string such as, "Hello Find Me World".

    Also, I don't suppose that anyone here has a function that they wouldn't mind sharing that will find ALL occurences of a string between 2 strings and return in array format?
     
  2. w84it

    w84it Regular Member

    Joined:
    Jun 18, 2009
    Messages:
    442
    Likes Received:
    752
    why not do a replace (chr(34)+chr(34),chr(34)) to change all those "" into " and then your function should work?
     
  3. gimme4free

    gimme4free Executive VIP Jr. VIP Premium Member

    Joined:
    Oct 22, 2008
    Messages:
    1,879
    Likes Received:
    1,932
    Managed to get some help over at SF on this one.

    Changing line:
    Dim istop As Integer = InStr(istart, haystack, needle_two)

    To:
    Dim istop As Integer = InStr(istart + Len(needle), haystack, needle_two)

    Fixed the issue :)
     
  4. Hydrogen

    Hydrogen Newbie

    Joined:
    Dec 30, 2009
    Messages:
    39
    Likes Received:
    23
    Occupation:
    Co-Owner of AdvertMarketing
    Home Page:
    I use my function all the time to parse HTML with it (mine is the Function 2 example you have above.

    This is how to parse out html value between quotes

    We'll assume this is your strHTML variable
    Code:
    <input type="hidden" name="testsubmit" id="testsubmit" value="findme"  />
    
    and we wanted to get the value testsubmit from the name field
    all you have to do is this

    Code:
    Dim needle as string = "name="""
    Dim needle_two as string = """ id="""
    dim strName as string = GetBetween(strHTML, needle, needle_two)
    
    what is happening here is in order to pass along the escape character " in this case you must encase it in quotes at each point it appears in your needle string. so name=" turns into "name=""" which effectively passes the start quote along to your parsing function GetBetween. Same thing goes for the end. I like to be sure with my ending needle so I included part of the next attribute in the closing needle but if you wanted to trim it down you could have made needle_two this """" which is (1) "

    I hope that helps out man.
     
    • Thanks Thanks x 1
    Last edited: Oct 11, 2011