Split a string while preserving values wrapped in quotes (.NET)
I'm looking for a way, in .NET, to split a string while ignoring split characters that are within quotes (or another delimiter). (This functionality would match what a typical CSV parser does if the split del开发者_如何学Pythonimiter is a comma.) I'm not sure why this ability isn't built into String.Split()
.
You can use a regular expression for that. Example:
string test = @"this,i""s,a"",test";
string[] parts =
Regex.Matches(test, @"(""[^""]*""|[^,])+")
.Cast<Match>()
.Select(m => m.Value)
.ToArray();
foreach (string s in parts) Console.WriteLine(s);
Output:
this
i"s,a"
test
Check out Marc's answer in this post:
Input array is longer than the number of columns in this table. Exception
He mentions a library you can use for this.
Using @Guffa's method, here is my full solution:
/// <summary>
/// Splits the string while preserving quoted values (i.e. 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, e.g. ',' 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;
}
If you also want to allow single quote (') then change the expression to @"(""[^""]""|'[^']'|[^\s])+".
If you want to remove the quotes from the string then change your Select to .Select(m => m.Value.Trim(new char [] {'\'','"'})).
精彩评论