拆分一个字符串,而包裹在报价preserving值(.NET) [英] Split a string while preserving values wrapped in quotes (.NET)
本文介绍了拆分一个字符串,而包裹在报价preserving值(.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在寻找一种方式,在.NET中,分割字符串,而忽略拆分字符是引号(或其他分隔符)之内。 (此功能将匹配是一个典型的CSV解析器,如果不拆分分隔符是一个逗号。)我不知道为什么这个能力没有内置 String.Split()
。
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 delimiter is a comma.) I'm not sure why this ability isn't built into String.Split()
.
推荐答案
您可以使用常规的前pression了点。例如:
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);
输出:
this
i"s,a"
test
这篇关于拆分一个字符串,而包裹在报价preserving值(.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文