Using the @Guffa method, here is my complete solution:
/// <summary> /// Splits the string while preserving quoted values (ie instances of the delimiter character inside of quotes will not be split apart). /// Trims leading and trailing whitespace from the individual string values. /// Does not include empty values. /// </summary> /// <param name="value">The string to be split.</param> /// <param name="delimiter">The delimiter to use to split the string, eg ',' for CSV.</param> /// <returns>A collection of individual strings parsed from the original value.</returns> public static IEnumerable<string> SplitWhilePreservingQuotedValues(this string value, char delimiter) { Regex csvPreservingQuotedStrings = new Regex(string.Format("(\"[^\"]*\"|[^{0}])+", delimiter)); var values = csvPreservingQuotedStrings.Matches(value) .Cast<Match>() .Select(m => m.Value.Trim()) .Where(v => !string.IsNullOrWhiteSpace(v)); return values; }
source share