472,090 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,090 developers and data experts.

String Class Split Behavior

297 100+
For those of you who have graduated from VB6 to VB.NET; one of the things you have undoubtedly encountered is how functionality you expected to be the same, or similar to VB6 turned out completely different in VB.NET. One of those I recently encountered is the String Class Split method.

In VB6 Split is a function:
Expand|Select|Wrap|Line Numbers
  1. Dim MySplittableString as String
  2. MySplittableString = "one,two,three,four,five"
  3. Dim MyArray() as String
  4. MyArray = Split(MySplittableString, ",")
In VB6 the code above would leave you with a 5 element array.

MyArray(0) = "one"
MyArray(1) = "two"

Also in VB6 you could specify a longer than one-character string with which to split a delimited string.

Expand|Select|Wrap|Line Numbers
  1. Dim MySplittableString as String
  2. MySplittableString = "one<split>two<split>three<split>four<split>five"
  3. Dim MyArray() as String
  4. MyArray = Split(MySplittableString, "<split>")
In VB6 this worked equally well.

Enter the Split method in the VB.NET String Class. You might expect it to work similarly, but it does not.

In the code below...
Expand|Select|Wrap|Line Numbers
  1. Dim MySplittableString as String = "one<split>two<split>three<split>four<split>five"
  2. Dim MyArray() as String = MySplittableString.Split"<split>")
...the split method ignores all but the first character - which it uses as the sole delimiter. So in the code above you get:

MyArray(0) = ""
MyArray(1) = split>one
MyArray(2) = split>two

What a mess...

In VB.NET you can pass the split method a string, but it ignores all but the first character. You can pass in an array of chars(), and it will use each of them as separate delimiters. That's handy if you need to split a sting that uses multiple delimiters.

Unfortunately you cannot use the String Class Split Method in VB.NET to do anything like the Split function in VB6.

You can, however, revert to the Classic VB Compatibility Split function - but some of us like to avoid that as we expand our knowledge base of canonical .NET.

Personally - I think MS screwed up here.
The VB.NET product manager wisely wanted to allow users to specify multiple delimiters, so s/he asked for an overloaded method that allows the user to pass an array of chars. Inside the method the string being parsed is likely being handled like an array of chars for speed and a simple comparison is being done.
The requirement was also to allow passing in a string of one or more characters. I bolded that because that is what it says in the MSDN Documentation for the String Class Split method. I think the person implementing the method saw this to mean that they should treat it the same as an array of chars and use individual characters from the string as separate delimiters. I also think they screwed up in the internal conversion to a char array and only took the first character in the string.
I think this is a shame because the days of delimiting text with a single character is a 1980s approach. I would have though that the implementation of the String.Split method would have recognized a more up-to-date set of requirements. I think MS should fix this. They won't break anything (or much) - mainly because I don't think anyone is using the String.Split method with a string longer than 1 since it doesn't work anyway.

A workaround!
Here's what I do.

Expand|Select|Wrap|Line Numbers
  1. Dim DelimiterSet() As Char = {"|"c, "~"c, "`"c, "└"c, "■"c}
  2. Dim Delimiter(0) As Char
  3. Dim I As Integer = 0
  4. For I = 0 To 4
  5.     If MySplittableString.Contains(DelimiterSet(I)) = False Then
  6.        Delimiter(0) = DelimiterSet(I)
  7.        Exit For
  8.      End If
  9. Next
  10. Dim MyArray() As String = MySplittableString.Replace("<split>", Delimiter(0).ToString).Split(Delimiter, StringSplitOptions.RemoveEmptyEntries)
The code above first checks the string needing to be split for the existence of a single character. Single character delimiting is risky today - so it's important to find a unique character that is not in the string you want to split. The code above loops through 5 possible characters to use - you can extend that if you wish. When it finds one that does NOT exist it will now replace the string (in my example "<split>") with the unique single character. It will then use the String.Split method to return an array.

That's a lot of code - but it does avoid the old VB6 code and seems relatively performant. There may be faster ways - but my requirements are fairly simple.

Jun 13 '10 #1
0 5910

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by JustSomeGuy | last post: by
23 posts views Thread by YinTat | last post: by
4 posts views Thread by Carl Youngblood | last post: by
27 posts views Thread by djake | last post: by
13 posts views Thread by Rob Meade | last post: by
3 posts views Thread by bob | last post: by
7 posts views Thread by craigkenisston | last post: by
7 posts views Thread by dragoncoder | last post: by
3 posts views Thread by mike7411 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.