Sometimes when spliting strings, the default String.Split method is not what you need.
String.Split will just split the string at each delimiter. Sometimes you may want to ignore delimiters inside double quotes. Also, you may want to ignore multiple delimiters in a row (such as multiple spaces) to relax input requirements. The following method does both. You can set any number of delimiters by passing in the a string and ignore delimiters inside double quotes. For example, the following string and delimiters will split into four fields.
Delims: ", " (comma and space)
String: "one two",,three, four, "five, six"
/// Splits any string using seperators string. This is different from the
/// string.Split method as we ignore delimiters inside double quotes and
/// will *ignore multiple delimiters in a row (i.e. "One,,,,two" will split
/// to two fields if comma is a delimiter).
/// Delims: " \t," (space, tab, comma)
/// Input: "one two" three four,five
/// Returns (4 strings):
/// one two
/// <param name="text">The string to split.</param>
/// <param name="delimiters">The characters to split on.</param>
public static string SplitQuoted(string text, string delimiters)
// Default delimiters are a space and tab (e.g. " \t").
// All delimiters not inside quote pair are ignored.
// Default quotes pair is two double quotes ( e.g. ‘""’ ).
if ( text == null )
throw new ArgumentNullException("text", "text is null.");
if ( delimiters == null || delimiters.Length < 1 )
delimiters = " \t"; // Default is a space and tab.
ArrayList res = new ArrayList();
// Build the pattern that searches for both quoted and unquoted elements
// notice that the quoted element is defined by group #2 (g1)
// and the unquoted element is defined by group #3 (g2).
string pattern =
@"([^" + delimiters + @"]+)";
// Search the string.
foreach ( System.Text.RegularExpressions.Match m in System.Text.RegularExpressions.Regex.Matches(text, pattern) )
//string g0 = m.Groups.Value;
string g1 = m.Groups.Value;
string g2 = m.Groups.Value;
if ( g2 != null && g2.Length > 0 )
// get the quoted string, but without the quotes in g1;
Note: As we ignore multiple delimiters in a row, you will have to explicitly set an empty field if you want an empty field. Such as one,"",two, three. This is because one,,two,three would only split to three fields, not four.