.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 has been a Microsoft MVP in C# since 2004.  He is also the co-founder of EggHeadCafe. Robbe enjoys scuba diving with the folks at wet-n-fla.

button
 
Article Discussion: .NET string.Split("::") Bug With Duplicate Delimiters
Robbe Morris posted at 19-Aug-08 04:56
Original Article

 
In the interest of completeness...
Peter Bromberg replied to Robbe Morris at 19-Aug-08 07:52
namespace stringsplit
{
    class Program
    {
        static void Main(string[] args)
        {
            TestSplit split = new TestSplit();
            Console.ReadLine();
        }
    }

    class TestSplit
    {
        string _test = "Note:  This is a string of text :: delimited by a double semi-colon.";
        /// <summary>
        /// Demonstrate some methods of splitting strings on multiple lines.
        /// </summary>
        public TestSplit()
        {
            // 1.
            // Split the string _test on double colon using Regex. The return value from Split
            // will be a string[] array. 
            string[] lines = Regex.Split(_test, "::");

            // 2.
            // Use a new char[] array of two characters (: and :) to break
            // lines from _test into separate strings. Use "RemoveEmptyEntries"
            // to make sure no empty strings get put in the string[] array.
            string[] lines2 = _test.Split(new char[] { ':', ':' },
                StringSplitOptions.RemoveEmptyEntries);

            // 3.
            // Same as the previous example, but uses a new string of 2 characters.
            // Will not return any empty strings, so "None" is an OK value for
            // StringSplitOptions.
            string[] lines3 = _test.Split(new string[] { "::" },
                StringSplitOptions.None);
            Console.WriteLine("lines--->" +lines[0] +" "+ lines[1]);
            Console.WriteLine("lines2-->" + lines2[0] + " " + lines2[1] +" "+lines2[2]);
            Console.WriteLine("lines3-->" + lines3[0] + " " + lines3[1]);
        }
    }
}
OUtput:
lines--->Note:  This is a string of text   delimited by a double semi-colon.
lines2-->Note   This is a string of text   delimited by a double semi-colon.
lines3-->Note:  This is a string of text   delimited by a double semi-colon.
Observe that option "lines2" actually produces three elements in order to get the entire string.

 
Line 2 removes the initial semi-colon next to Note and it should not
Robbe Morris replied to Peter Bromberg at 19-Aug-08 08:37

eop


 
the real problem
noneof yourbusiness replied to Robbe Morris at 27-Aug-08 03:06
The real problem was that you programmed it to your assumption that it would return 2 items. If you programmed the writeline statement using a foreach, you would have found that the real problem wasn't that it truncated.

foreach (string s in text)
{
    Console.WriteLine(s);
}


 
This is no bug...
123456 Nonymous replied to Robbe Morris at 28-Aug-08 10:35

This is no bug... at least not in the .NET framework. It is a bug in your code however.

If you want to split your string by "::" you should use text.Split("::"). With the ToCharArray in place you are basically using text.Split(new char[] { ':', ':' }), which splits the string by any of the characters in the array (which in this case are the same). In your example this results in 4 values:
- "Note"
- "  This is a string of text "
- ""
- " delimited by a double semi-colon."

Check out the documentation and examples of the String.Split method in the MSDN Library...


 
This is the way to do it
web surfer replied to 123456 Nonymous at 18-Dec-08 06:54
123456 Nonymous is correct but he made a mistake because the following will not work:

text.Split("::")

if you want to split using a string you have to do it like this:

text.Split(new String[]{"::"}, StringSplitOptions.None)

Regards.

promotion
Silverlight    WPF    WCF    WWF    LINQ   
JavaScript    AJAX    ASP.NET    XAML   
C#    VB.NET    VB 6.0    GDI+    IIS    XML   
.NET Generics    Anonymous Methods    Delegate   
Visual Studio .NET    Expression Blend    Virus   
Windows Vista    Windows XP    Windows Update   
Windows 2003 Server    Windows 2008 Server   
SQL Server    Microsoft Excel    Microsoft Word   
SharePoint    BizTalk    Virtual Earth   
.NET Compact Framework    Web Service   

"Everything" RSS / ATOM Feed Parser
How to send and receive messages through message queuing in .Net
How to Read text file as database
SQL Server 2005 Paging Performance Tip
Display code of web page.
Fully Scalable Excel File Importer class for .net using Microsoft Jet driver
Generic Chart Color Manager class that can be used for any charts
Helper class to style the infragistics wingrid
Using Reflection to detemine as Assembly Info in and out.
Helper class to play with Window (Owners and position)
Resolving displayname from the culture using the XmlLanguage and LanguageSpecificStringDictionary class
Manipulate file attributes in VB.NET
Forms Based Authentication Filtered Content Editor for SharePoint
How to create a Tree View of the Windows Folder and extract all the file-folder info.
How to use AssemblyInfo.cs file in win forms to provide much needed information on Assemblies
Sorting In Datagrid
Helper class to work with NativeMethods in the native api's
Silverlight Line Of Business Applications With Offline WPF Versions
C# : Database monitoring system using XML file
C# : Adding ComboBox to ListView SubItem
Sum of Numbers Captcha: Keeping it Simple
C# Create a Piechart for the specified Hard Disk Drive Utilization
Extension Methods for DataSet and DataTable that makes tasks easier
Accessing IIS Hosted WCF Services from PHP
Helper class that provides most commonly used Extension Methods for DateTime object
Helper class to work with a Status Bar in WPF.
Finding Unmatched Records in Dataset Tables Using Linq
Silverlight Toolkit: Autocomplete TextBox Stock Symbols and Chart
COOL Auto Complete textbox using javascript
Creating a Serializable Log Entry for Microsoft Enterprise Library to log to a Database
ASP.NET Searching Values in Datagrid