Ro Machet
Ro Machet

Reputation: 11

get all strings between 2 strings

   Public Function GetStringBetween(ByVal InputText As String, _
  ByVal starttext As String, _
  ByVal endtext As String)

        Dim lnTextStart As Long
        Dim lnTextEnd As Long

        lnTextStart = InStr(StartPosition, InputText, starttext, vbTextCompare) + Len(starttext)
        lnTextEnd = InStr(lnTextStart, InputText, endtext, vbTextCompare)
        If lnTextStart >= (StartPosition + Len(starttext)) And lnTextEnd > lnTextStart Then
            GetStringBetween = Mid$(InputText, lnTextStart, lnTextEnd - lnTextStart)
        Else
            GetStringBetween = "ERROR"
        End If
    End Function
    Dim xa As String
        Dim x As String = WebBrowser1.DocumentText

Usage

  xa = GetStringBetween(x, TextBox1.Text, TextBox2.Text)

    MsgBox(xa)

I have tried many many diffrent ways to try to get all strings between 2 other strings with for each etc the site has more then 1 string with those 2 strings but i just get the first string between 2 strings help sorry its hard to explain :/

Upvotes: 0

Views: 4695

Answers (3)

Intellesense
Intellesense

Reputation: 1

Public Function GetBetween(IStringStr As String, IBefore As String, IPast As String)
    On Error Resume Next
    Dim iString As String
    iString = IStringStr
    iString = Right(iString, Len(iString) - InStr(iString, IBefore) - Len(IBefore) + 1)
    iString = Mid(iString, 1, InStr(iString, IPast) - 1)
    GetBetween = iString
End Function

In old style right is depreciated as you know

Upvotes: 0

Steve
Steve

Reputation: 216243

Probably the most concise way to write that code is through RegEx, but it could be overkill.
This is a simple method that do the same thing using string.IndexOf

Public Function GetStringBetween(ByVal InputText As String, _
                                 ByVal starttext As String, _
                                 ByVal endtext As String)

    Dim startPos As Integer
    Dim endPos As Integer
    Dim lenStart As Integer
    startPos = InputText.IndexOf(startText, StringComparison.CurrentCultureIgnoreCase)
    if startPos >= 0 Then
        lenStart = startPos + starttext.Length
        endPos = InputText.IndexOf(endtext, lenstart, StringComparison.CurrentCultureIgnoreCase)
        If endPos >= 0 Then
            return InputText.Substring(lenStart, endPos - lenStart)
        End If
    End If
    return "ERROR"
End Function

Upvotes: 2

Onur Gazioğlu
Onur Gazioğlu

Reputation: 511

You can use regex. For ex: (word1).*(word2)

Upvotes: 0

Related Questions