简体   繁体   中英

Parsing CSV strings (not files) in C#

Using C#, I need to parse a CSV string that doesn't come from a file. I've found a great deal of material on parsing CSV files, but virtually nothing on strings. It seems as though this should be simple, yet thus far I can come up only with inefficient methods, such as this:

using Microsoft.VisualBasic.FileIO;

var csvParser = new TextFieldParser(new StringReader(strCsvLine));
csvParser.SetDelimiters(new string[] { "," });
csvParser.HasFieldsEnclosedInQuotes = true;

Are there good ways of making this more efficient and less ugly? I will be processing huge volumes of strings, so I wouldn't want to pay the cost of all the above. Thanks.

Here is a lightly tested parser that handles quotes

List<string> Parse(string line)
{
    var columns = new List<string>();
    var sb = new StringBuilder();

    bool isQuoted = false;
    int nQuotes = 0;
        
    foreach(var c in line)
    {
        if (sb.Length == 0 && !isQuoted && c == '"')
        {
            isQuoted = true;
            continue;
        }
            
        if (isQuoted)
        {
            if (c == '"')
            {
                nQuotes++;
                continue;
            }
            else
            {
                if (nQuotes > 0)
                {
                    sb.Append('"', nQuotes / 2);
                    if (nQuotes % 2 != 0)
                    {
                        isQuoted = false;
                    }
                    nQuotes = 0;
                }
            }
        }
        if (!isQuoted && c == ',')
        {
            columns.Add(sb.ToString());
            sb.Clear();
            continue;
        }

        sb.Append(c);
    }

    if (nQuotes > 0)
    {
        sb.Append('"', nQuotes / 2);
    }

    columns.Add(sb.ToString());

    return columns;
}

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