0
   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 :/

3
  • 2
    What are "all strings between two strings"? Do you want the substring between string1 and string2? Multiple strings presumes that you want to split by white-space (for example). Can you show an example and a desired result? Commented May 6, 2013 at 9:00
  • 2
    This code looks like VB6... maybe it would be interesting for you to learn the advantages that Vb.NET could provide Commented May 6, 2013 at 9:22
  • Looks like you're trying to do a school assignment? If not, you keep repeating you need to get all strings between two strings, what does that mean exactly? Give us two sample strings and what the result you expect is, and maybe we can give you useful help. Commented May 6, 2013 at 12:44

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

no i need all strings my the code i use works but it only get the first string between the other 2 strings but the other ones in the text but it only gives me the first one. You know like for each string i need all the strings in the text thats between the 2 strings
0

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

Comments

0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.