1

I'm trying to accomplish something fairly simple in VB that I do everyday in JavaScript.

I need to parse text between two strings (HTML tags mainly) that have multiple occurrences.

Sample Data:

<tag>test</tag>
<tag>test2</tag>
<tag>test3</tag>

If I wanted to grab the data in the 2nd <tag> in JavaScript I would simply do this:

var result = string.split('<tag>')[2].split('</tag>')[0];

And the only way I seem to get that to work in VB looks like this...

Dim from = string.IndexOf("<tag>")
Dim [to] = string.IndexOf("</tag>", from)
Dim result = string.Substring(from + "<tag>".Length, [to] - from - "<tag>".Length)

Mind you that is only the first occurrence in VB and already the code looks ridiculous in comparison... I didn't even want to figure out the 2nd occurrence until I find out this is my only solution. Thanks

1 Answer 1

2

You can do about the same thing in VB using the 'Split' method on the String.

    Dim sx As String = "<tag>test</tag> <tag>test2</tag> <tag>test3</tag> "
    Dim sp As String = sx.Split(New [String]() {"<tag>"}, StringSplitOptions.RemoveEmptyEntries)(1).Split(New [String]() {"</tag>"}, StringSplitOptions.RemoveEmptyEntries)(0)
Sign up to request clarification or add additional context in comments.

2 Comments

After your response I dug more into it... And turns out you are correct :) I was using the wrong "Split" function trying to use indexOf/Substring. Solution: Regex.Split(string, "<tag>")(1).Split("</tag>")(0)
check my new issue with splitting here: stackoverflow.com/questions/10458032/…

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.