Splitting a CSV and excluding commas within elements

前端 未结 4 1808
夕颜
夕颜 2020-12-21 06:31

I\'ve got a CSV string an I want to separate it into an array. However the CSV is a mix of strings and numbers where the strings are enclosed in quotes and may contain comma

相关标签:
4条回答
  • 2020-12-21 07:04

    I would suggest using one of the following solutions, was just testing a few of them (hence the delay):-

    1. Regex matching commas not found within an enclosing double aprostophe
    2. A Fast CSV Reader - for read CSV only
    3. FileHelpers Library 2.0 - for read/write CSV

    Hope this helps.

    0 讨论(0)
  • 2020-12-21 07:13

    String.Split() is icky for this. Not only does it have nasty corner cases where it doesn't work like the one you just found (and others you haven't seen yet), but performance is less than ideal as well. The FastCSVReader posted by others will work, there's a decent csv parser built into the framework (Microsoft.VisualBasic.TextFieldParser), and I have a simple parser that behaves correctly posted to this question.

    0 讨论(0)
  • 2020-12-21 07:19

    The "A Fast CSV Reader" article on Code Project. I've used it happily many times.

    0 讨论(0)
  • 2020-12-21 07:21

    It's not the most elegant solution, but the quickest if you want to just quickly copy and paste code (avoiding having to import DLLs or other code libraries):

        private string[] splitQuoted(string line, char delimeter)
        {
            string[] array;
            List<string> list = new List<string>();
            do
            {
                if (line.StartsWith("\""))
                {
                    line = line.Substring(1);
                    int idx = line.IndexOf("\"");
                    while (line.IndexOf("\"", idx) == line.IndexOf("\"\"", idx))
                    {
                        idx = line.IndexOf("\"\"", idx) + 2;
                    }
                    idx = line.IndexOf("\"", idx);
                    list.Add(line.Substring(0, idx));
                    line = line.Substring(idx + 2);
                }
                else
                {
                    list.Add(line.Substring(0, Math.Max(line.IndexOf(delimeter), 0)));
                    line = line.Substring(line.IndexOf(delimeter) + 1);
                }
            }
            while (line.IndexOf(delimeter) != -1);
            list.Add(line);
            array = new string[list.Count];
            list.CopyTo(array);
            return array;
        }
    
    0 讨论(0)
提交回复
热议问题