简体   繁体   中英

c# comma separated string to list

I have seen a bunch of examples of handling comma separated lists using Split(','), for a string like:

string csv = "1,2,3,4,a,5";
List<string> parts = csv.Split(',').ToList<string>();

But what happens, and how would you handle a CSV string like this:

string csv = '"a,b","c,d","e","f",g,h,i';

I would like to be able to parse the csv string into a list or an array, it is more about knowing the best way to handle the string elements that contain a comma in them, and not have a simple parser like Split to get confused around the extra comma.

Alexei's comment was helpful, but after trying it in my real life example, his solution ran into some snags.. So for extra credit.. This is a more accurate csv string that needs to be parsed.

csv = '"name 1" <title 1>, "name, 2" <title 2>, name 3 <title 3>, name 4 <title 4>, name 5, name 6';

The values would be split into:

"name 1" <title 1>
"name, 2" <title 2> 
name 3 <title 3>
name 4 <title 4> 
name 5
name 6

Thank you

Use regex to do it. It is the quickest and easiest way. Make sure to add System.Text.RegularExpressions to your references. Put the single and double quotes as your special characters and it will strip them out. See this post for details.

Regex remove special characters

Plain C# solution for the second string. It assumes it is a valid CSV, with no blanks between the tokens. I do not think it is the fastest, but it should be enough for reasonable sized arrays (~MB). Complexity is O(n) :

private static void AddToBuilder(IList<string> parts, StringBuilder sb)
{
    if (sb.Length > 0)
        parts.Add(sb.ToString());

    sb.Clear();
}

static void Main(string[] args)
{
    string csv = "\"a,b\",\"c,d\",\"e\",\"f\",g,h,i";
    var parts = new List<string>();
    bool innerString = false;
    var sb = new StringBuilder();
    foreach (var c in csv)
    {
        if (c == '\"')
        {
            if (innerString)
                AddToBuilder(parts, sb);

            innerString = !innerString;
            continue;
        }

        if (c == ',' && !innerString)
        {
            AddToBuilder(parts, sb);
            continue;
        }

        sb.Append(c);
    }

    AddToBuilder(parts, sb);

If by any chance a third party CSV parser is not an option, another alternative is Microsoft.VisualBasic.FileIO.TextFieldParser (needs Reference to Microsoft.VisualBasic ):

string CSV = "\"a,b\",\"c,d\",\"e\",\"f\",g,h,i";
string[] fields;

using (var sr = new System.IO.StringReader(CSV))  
using (var tfp = new Microsoft.VisualBasic.FileIO.TextFieldParser(sr)) {
    tfp.SetDelimiters(",");
    fields = tfp.ReadFields();
}

It also handles values that contain properly escaped quotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM