473,881 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String Class Split Behavior

297 Contributor
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 6296

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

Similar topics

by: JustSomeGuy | last post by:
I have a structure typedef struct { string a; string b; } atype; string ABC = ("123"); string DEF = ("456");
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);
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 how to add functionality to the string class. Rather than trying to inherit from the string class, the author was suggesting wrapping the string class in another class, like so: >Right !!!, we should not derive from string, but we should wrap...
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 initialized with a constructor like this
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 breakpoints) and i try to see the content of my std:: strings , i get this comment : "undefined value", that means that this type(std::string) compiles but doesn't work since i don't get real values in it...
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 class which derived from the String class, add the function there and then we would all use the new class which contained my function and all of the others etc....sounded relatively straight forward...
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. Consider the following ctor. It constructs a TtkString object with a string value of the integer contained withing. e.g TtkString one(456); cout << one << endl;
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" Console.WriteLine(MySpecialString.ToString()) // output "1234567890" i.e. trimes the "ABCDE"
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
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.
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.