.NET Find string in between other strings

Posted on 2011-10-30
Medium Priority
Last Modified: 2012-08-13
Hello, I have a program that downloads the source of a webpage. I need to get a string in between other strings. I want to specify the beginning and end, and then get the in between.

Question by:lean0nme
  • 5
  • 4
LVL 37

Expert Comment

ID: 37053223
Do a quick Google search on vb regular expressions and it will be easy.

Author Comment

ID: 37053227
I did and have gotten something which works, but for some reason breaks if there is a lot of text. Here is the code:

Function Mybetween(ByVal src As String, ByVal first As String, ByVal last As String) As String
        MsgBox("src is " & src)
        MsgBox("Start string is " & first)
        MsgBox("End string is " & last)
            src = src.Replace(first, "|")
            src = src.Replace(last, "|")
            Return src.Split(New Char() {"|"c})(1)

        Catch ex As Exception

        End Try

    End Function

    Function Between(ByVal src As String, ByVal first As String, ByVal last As String) As String
        Dim startloc As Integer = InStr(src, first, 0)
        Dim endloc As Integer = InStr(Strings.Mid(src, (startloc + 1)), last, 0)
        Return Mid(src, (startloc + 1), (endloc - 1))
    End Function
    Function between(ByVal src As String, ByVal c_start As Char, ByVal c_end As Char) As String
        Dim ret = String.Empty
        Dim idxStart = src.IndexOf(c_start)
        If idxStart <> -1 Then
            idxStart += idxStart
            Dim idxEnd = src.IndexOf(c_end, idxStart)
            If idxEnd <> -1 Then
                ret = src.Substring(idxStart, idxEnd - idxStart)
            End If
        End If
        Return ret
    End Function

Open in new window

LVL 83

Expert Comment

ID: 37053229
You have multiple options.

1) Regular Expressions
2) HTML Parsing
3) String manipulation.

For 3, you can use indexof, substring etc to find the location of string. Indexof takes a parameter to specify the start of search. So index of first string can be used to find index of search string which occurs after the first string.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 37

Expert Comment

ID: 37053293
Your code has three different attempts in it. The first one looks the simplest and should work.

When you call MyBetween, does it give an error? It looks like it should work.

Author Comment

ID: 37053331
Sorry, the code I'm using is MyBetween. It works, but stops working with a lot of text for some reason.
LVL 37

Expert Comment

ID: 37053370
Add this line
 MsgBox ex.Message
Right after the line that says Catch ex As Exception

Then you can probably see why it isn't working.
Otherwise, what is it doing when it "stops working"?

Author Comment

ID: 37053378
It doesn't give an error, just returns a null result.
LVL 37

Accepted Solution

TommySzalapski earned 1500 total points
ID: 37053384
Output the src and see if it's finding the other strings.

Author Comment

ID: 37053402
I used the Between method and it works. So weird because it didn't work before which is why I was using mybetween.

Thank you.

Author Closing Comment

ID: 37053405
Thanks for the help!

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question