473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,396 developers and data experts.

String Class Split Behavior

!NoItAll
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, ",")
  5.  
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>")
  5.  
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>")
  3.  
...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.
<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.
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.
</speculation>
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)
  11.  
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.

Des
Jun 13 '10 #1
0 6192

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

Similar topics

11
by: JustSomeGuy | last post by:
I have a structure typedef struct { string a; string b; } atype; string ABC = ("123"); string DEF = ("456");
23
by: YinTat | last post by:
Hi, I learned C++ recently and I made a string class. A code example is this: class CString { public: inline CString(const char *rhs) { m_size = strlen(rhs);
4
by: Carl Youngblood | last post by:
I imagine this subject has probably been brought up numerous times. Forgive me for bringing it up again. I was googling through old posts on this newsgroup about it and found a good suggestion on...
27
by: djake | last post by:
In the stroustrup C++ programming language (third edition) i can find example like this: string s1= "Hello"; So I imagine string is a standard class. Furthermore the class in the example is...
1
by: bor_kev | last post by:
Hi! I am developing under Microsoft visual Studio 2005 in C++. In my code I use the std:: string class and when I compile I don't have any mistakes. However, when I debug my program (with...
13
by: Rob Meade | last post by:
Hi all, I have written a small ProperCase function which I would like to make available to our team at work through our common class library. A colleague mentioned that I could write a new...
3
by: bob | last post by:
Hi, I'm looking at a legacy string class thats been in use here for a while and I'd like to check out any options available to optimise it. I see a couple of constructors that look dubious....
7
by: craigkenisston | last post by:
I need an special string that have a fixed capacity and that automatically(silently) trims extra space. MySpecialString str = new MySpecialString(10); str = "1234567890ABCDE" ...
7
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
3
by: mike7411 | last post by:
When you use she STL string class and the c_str() function, how does the memory returned by c_str() get allocated and destroyed? Thank you.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.