1

I was getting weird results when doing multiple splits on a string, so I decided to make a simple test to figure out what was going on

testString "1234567891011121314151617181920"

If I wanted to get whats between 10 to 20 in Javascript I would do this:

var results = testString.split("10")[1].split("20")[0]

Which would return 111213141516171819

However when I do this in VB I get 111

Split(testString,"10")(1).Split("20")(0)

It seems the 2nd split is only recognizing the first character no matter what I put.

So it's stopping when it finds the next "2" in the string, even "2abc" would have the same outcome even though that string doesn't even exist.

3
  • 2
    What happens when you wrap the second split so it's fashioned like the first one? i.e. Split( Split(testString,"10")(1), "20")(0)" Commented May 5, 2012 at 1:40
  • That worked perfectly Marc! That's the easiest syntax I have seen from other solutions posted... Thanks! Commented May 5, 2012 at 2:39
  • glad you got it worked out. I added my solution as an answer below. Commented May 5, 2012 at 14:12

4 Answers 4

2

String.Split does not have an overload that takes only a String. The argument is a Char array or String array. Your string is probably being converted to a char array. Explicitly pass a string array like so:

testString.Split(New String() { "10" }, StringSplitOptions.None)

Sign up to request clarification or add additional context in comments.

Comments

1

Try wrapping the second split so it's fashioned like the first one, i.e.:

Split( Split(testString,"10")(1), "20" )(0)"

Comments

1

Vb treats the delimiter argument only as a single character.

Comments

1

This is a tricky scenario that I have seen trip people up before, so I think it is worth a little more explanation than the other answers give. In your original format Split(testString,"10")(1).Split("20")(0), you are unknowingly using two DIFFERENT Split functions.

The first Split(testString,"10") is using the Microsoft.VisualBasic.Strings.Split function, which takes String type parameters. http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.split(v=vs.110).aspx

The second .Split("20")(0) is using System.String.Split method, which does not have an overload that takes a String parameter. http://msdn.microsoft.com/en-us/library/System.String.Split(v=vs.110).aspx

So what was happening is:

  1. Split(testString,"10") uses Microsoft.VisualBasic.Strings.Split, which returns new String() {"123456789", "11121314151617181920"}
  2. (1) means get 1st position of the returned array, which is "11121314151617181920"
  3. "11121314151617181920".Split("20")(0) uses System.String.Split, and attempts to split on string separator "20"
  4. NOTE: The string "20" param gets implicitly converted to a char "2" because the only single parameter overload of String.Split has a signature of Public Function Split (ParamArray separator As Char()) As String(). The ParamArray parameter option allows you to pass a comma delimited list of values into the function, similar to how String.Format works with a dynamic # of replacement values. http://msdn.microsoft.com/en-us/library/538f81ec.aspx
  5. Step 3 code becomes "11121314151617181920".Split(new Char() {CChar("20")})(0), which using literal values is "11121314151617181920".Split(new Char() {"2"c})(0). The result is {"111", "13141516171819", "0"}. Get the 0th position, returns "111".

So to avoid confusion, you should convert your code to use the same version of Split on both sides.

Either of the 2 examples below should work:

Example 1: Using Microsoft.VisualBasic.Strings.Split:

Split( Split(testString,"10")(1), "20" )(0)

Example 2: Using System.String.Split:

testString _ 
    .Split(New String() {"10"}, StringSplitOptions.None)(1) _ 
    .Split(New String() {"20"}, StringSplitOptions.None)(0)

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.