Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Split() is opposite of Join()? Not!

In VB6 this was easy:

Split("", "|") gives an empty array.
Split("1<-->2<-->3", "<-->") gives the array {"1", "2", "3"}
Join(array, "<-->") gives the string "1<-->2<-->3"

but in C#:

"".Split('|') gives an array with one, but empty element.
Split uses a char as delimeter, Join a string. So you can join an array into a string using another string of desired length as delimeter, but you can't split the string using the same delimeter as Split() accepts chars only, multiple chars means multiple delimeters.

So how on earth do I split a string with a string delimeter longer than 1?

Thomas Eyde
Wednesday, October 01, 2003

By using Regex.Split instead. There's no other way, unless you want to manually traverse the input string and look for occurrences of the splitter string. Here's how it works (in C#):

string input = "A<->B<->C";
string separator = "<->";

string search = string.Format("({0})", Regex.Escape(separator));
Regex regex = new Regex(search, RegexOptions.ExplicitCapture);
string[] output = regex.Split(input);

Chris Nahr
Thursday, October 02, 2003

Duh, there are static methods that make it even simpler... just use this:

string[] output = Regex.Split("A<->B<->C", Regex.Escape("<->"));

Regex.Escape ensures that all characters which have special meaning in regular expressions are properly escaped. It's not necessary in the example, though.

Chris Nahr
Thursday, October 02, 2003

Thanks! A little hard to find, but this is as easy as it gets in C#. I suspected I needed RegEx, but I never got the hang of it so I never thought it would be this easy.

Thomas Eyde
Thursday, October 02, 2003

Well, you could just add a reference to Microsoft.VisualBasic and use the VB library. Works fine.

Dave Rothgery
Thursday, October 02, 2003

Maybe, but it's a dirty hack for fixing a stupid inconsistency: Join() uses a string, Split() a char. Hello C# team, time to do a code review?

Thomas Eyde
Friday, October 03, 2003

I imagine the Framework team would have more interest in something like this than the C# team.  Besides, an array of char make perfect sense for Split because that's exactly how it uses that parameter -- it looks for *each* char in the array, not for the full string. 

SomeBody
Saturday, October 04, 2003

Perfect sense when that is what you need, and sometimes it is. But they could bother to overload it whith a string parameter so it would work just as the opposite of Join().

I also realize that the special case when

  string[0] allItems == aString.Split('|');

is tricky. Which condition should aString fulfil to make the above true?

Thomas Eyde
Saturday, October 04, 2003

I think there would be globalisation issues with having String.Split taking strings -- so Regex.Split would be the way to go. Granted, a pointer in the docs would be helpful.

Duncan Smart
Saturday, October 04, 2003

What about:

string[] Split(string data, string separator)
{
    string separatorChar = '|';
    data = data.Replace(separator, separatorChar);
    return data.Split(separatorChar);
}

just anoter (dirty) way to do it
Tuesday, October 07, 2003

Except that that will also kill any | that happen to be in the target string.

The regex solution works. Why bother trying to find a "complexly clever" solution?

Brad Wilson (dotnetguy.techieswithcats.com)
Saturday, October 11, 2003

*  Recent Topics

*  Fog Creek Home