1

In a failed attempt to teach my daughter programming I taught her to make this

Public Shared Function getBetween(strSource As String, startSearchIndex As Integer, startSearch As String, endSearch As String) As (nextIndex As Integer, Content As String)
    Dim IndexStart = strSource.IndexOf(startSearch, startSearchIndex)
    Dim startContent = IndexStart + startSearch.Length
    Dim indexEnd = strSource.IndexOf(endSearch, startContent)
    Dim content = strSource.Substring(startContent, indexEnd - startContent)
    Dim nextIndex = indexEnd + endSearch.Length
    Return (nextIndex, content)

End Function

We know what the function does. Get strings between 2 strings in a big string.

I wonder if there is a built in function for this?

Samples:

If I use this test function

Public Shared Sub test(txt As TextBox)
    Dim source = "0123456789([012345])0123456789([ABCDEF])0123456789([GHIJK])"
    txt.Text += source + System.Environment.NewLine
    getBetween3Times(txt, source, "(", ")")
    getBetween3Times(txt, source, "([", "])")
    getBetween3Times(txt, source, "[", "]")
End Sub

Private Shared Sub getBetween3Times(txt As TextBox, source As String, startsearch As String, endsearch As String)
    Dim result = getBetween(source, 0, startsearch, endsearch)
    txt.Text += result.Content + System.Environment.NewLine
    result = getBetween(source, result.nextIndex, startsearch, endsearch)
    txt.Text += result.Content + System.Environment.NewLine
    result = getBetween(source, result.nextIndex, startsearch, endsearch)
    txt.Text += result.Content + System.Environment.NewLine
End Sub

It will produce

0123456789([012345])0123456789([ABCDEF])0123456789([GHIJK])
[012345]
[ABCDEF]
[GHIJK]
012345
ABCDEF
GHIJK
012345
ABCDEF
GHIJK
4
  • Can you provide an example? Commented Apr 1, 2021 at 12:47
  • Samples coming. I'll be right back Commented Apr 1, 2021 at 12:51
  • But i can give you already the answer, if it's working (it's looks so) then it's the closest you can get to "built in function". Commented Apr 1, 2021 at 12:54
  • So the answer is there is no built in function for this? Commented Apr 1, 2021 at 13:03

1 Answer 1

1

No, there is no built-in method, or yes, there are, but you use them already.

Maybe you want to handle invalid input. Also, your method includes the search-token, but you want the text between, so maybe this one is little bit better:

Public Shared Function GetTextBetween(strSource As String, startSearchIndex As Integer, startSearch As String, endSearch As String) As (nextIndex As Integer, Content As String)
    If startSearchIndex < 0 Orelse startSearchIndex >= strSource.Length Then Throw New ArgumentException(Nameof(startSearchIndex))
    Dim index = strSource.IndexOf(startSearch, startSearchIndex)
    If index = -1 Then Return (-1, Nothing)
    index += startSearch.Length
    Dim endIndex = strSource.IndexOf(endSearch, index+1)
    If endIndex = -1 Then Return (-1, Nothing)
    Dim content = strSource.Substring(index, endIndex - index)
    Return (endIndex + endSearch.Length, content)
End Function 

Dim sampleText = "It is the end of the world"
Dim result = GetTextBetween(sampleText, 0, "is ", " of") ' should be "the end"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. My code won't return the search token though. I tested it. Also the search strings may be longer than 1 character

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.