.NET string.Split("::") Bug With Duplicate Delimiters By Robbe Morris Printer Friendly Version View My Articles
|  |
In all my years of programming in C#, I've never run across this nice little bug in string.Split() and duplicate character delimiters. |
Until today, I never realized the .Split method of the string object didn't support splits on duplicate delimiters like :: or ||. You have to use RegEx and escape any RegEx specific characters that might be apart of your pattern.
string[] text = null;
string test = "Note: This is a string of text :: delimited by a double semi-colon.";
text = test.Split("::".ToCharArray());
Debug.WriteLine(text[0] + " " + text[1]);
text = System.Text.RegularExpressions.Regex.Split(test,
System.Text.RegularExressions.Regex.Escape("::"));
Debug.WriteLine(text[0] + " " + text[1]);
This code yields the following:
Note This is a string of text Note: This is a string of text delimited by a double semi-colon. |
| Biography |
Robbe is a 2004-2008 Microsoft MVP for C# and the .NET Evangelist for Alinean Inc.. He is also the co-founder of EggHeadCafe. Robbe enjoys scuba diving with the folks at wet-n-fla.
 |