![]() Welcome! and rules Joel on Software |
Split() is opposite of Join()? Not! In VB6 this was easy:
Thomas Eyde
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#):
Chris Nahr
Duh, there are static methods that make it even simpler... just use this:
Chris Nahr
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
Well, you could just add a reference to Microsoft.VisualBasic and use the VB library. Works fine.
Dave Rothgery
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
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
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().
Thomas Eyde
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
What about:
just anoter (dirty) way to do it
Except that that will also kill any | that happen to be in the target string.
Brad Wilson (dotnetguy.techieswithcats.com)
|