解析逗号分隔字符串在C#中的并发症 [英] Parse comma seperated string with a complication in C#

查看:171
本文介绍了解析逗号分隔字符串在C#中的并发症的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何从一个字符串,它是昏迷分隔但这里有一个复杂得子:如果什么子含有昏迷

I know how to get substrings from a string which are coma seperated but here's a complication: what if substring contains a coma.

如果一个子含有昏迷,新生产线或双引号整个子封装用双引号。

If a substring contains a coma, new line or double quotes the entire substring is encapsulated with double quotes.

如果一个子串包含双引号的双引号被转义与另一双引号。
最坏的情况是,如果我有这样的事情:

If a substring contains a double quote the double quote is escaped with another double quote. Worst case scenario would be if I have something like this:

first,"second, second","""third"" third","""fourth"", fourth"

在这种情况下,子字符串

In this case substrings are:


  • 第一个

  • 第二个,第二个

  • 第三个第三

  • 第四,第四

  • first
  • second, second
  • "third" third
  • "fourth", fourth

第二个,第二个封装用双引号,我不想在一个列表/阵列的双引号。

second, second is encapsulated with double quotes, I don't want those double quotes in a list/array.

第三第三封装用双引号,因为它包含双引号和那些与aditional的转义双引号。再次,我不希望在一个列表/阵列封装的双引号,我不想逃脱双引号的双引号,但我想原来的双引号这是子字符串的一部分。

"third" third is encapsulated with double quotes because it contains double quotes and those are escaped with aditional double quotes. Again I don't want the encapsulating double quotes in a list/array and i don't want the double quotes that escape double quotes, but I want original double quotes which are a part of the substring.

推荐答案

使用 TextFieldParser 方式一:

using (var reader = new StringReader("first,\"second, second\",\"\"\"third\"\" third\",\"\"\"fourth\"\", fourth\""))    
using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(reader))
{
    parser.Delimiters = new[] { "," };
    parser.HasFieldsEnclosedInQuotes = true;

    while (!parser.EndOfData)
    {
        foreach (var field in parser.ReadFields())
            Console.WriteLine(field);
    }
}

有关

first
second, second
"third" third
"fourth", fourth

这篇关于解析逗号分隔字符串在C#中的并发症的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆