In VB6 Split is a function:
Expand|Select|Wrap|Line Numbers
- Dim MySplittableString as String
- MySplittableString = "one,two,three,four,five"
- Dim MyArray() as String
- MyArray = Split(MySplittableString, ",")
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
- Dim MySplittableString as String
- MySplittableString = "one<split>two<split>three<split>four<split>five"
- Dim MyArray() as String
- MyArray = Split(MySplittableString, "<split>")
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
- Dim MySplittableString as String = "one<split>two<split>three<split>four<split>five"
- Dim MyArray() as String = MySplittableString.Split"<split>")
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.
<speculation>
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.</speculation>
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
- Dim DelimiterSet() As Char = {"|"c, "~"c, "`"c, "└"c, "■"c}
- Dim Delimiter(0) As Char
- Dim I As Integer = 0
- For I = 0 To 4
- If MySplittableString.Contains(DelimiterSet(I)) = False Then
- Delimiter(0) = DelimiterSet(I)
- Exit For
- End If
- Next
- Dim MyArray() As String = MySplittableString.Replace("<split>", Delimiter(0).ToString).Split(Delimiter, StringSplitOptions.RemoveEmptyEntries)
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.
Des