How to split csv whose columns may contain ,

前端 未结 8 1322
我寻月下人不归
我寻月下人不归 2020-11-22 06:35

Given

2,1016,7/31/2008 14:22,Geoff Dalgas,6/5/2011 22:21,http://stackoverflow.com,"Corvallis, OR",7679,351,81,b437f461b3fd27387c5d8ab47a293d35,34

8条回答
  •  生来不讨喜
    2020-11-22 07:07

    I see that if you paste csv delimited text in Excel and do a "Text to Columns", it asks you for a "text qualifier". It's defaulted to a double quote so that it treats text within double quotes as literal. I imagine that Excel implements this by going one character at a time, if it encounters a "text qualifier", it keeps going to the next "qualifier". You can probably implement this yourself with a for loop and a boolean to denote if you're inside literal text.

    public string[] CsvParser(string csvText)
    {
        List tokens = new List();
    
        int last = -1;
        int current = 0;
        bool inText = false;
    
        while(current < csvText.Length)
        {
            switch(csvText[current])
            {
                case '"':
                    inText = !inText; break;
                case ',':
                    if (!inText) 
                    {
                        tokens.Add(csvText.Substring(last + 1, (current - last)).Trim(' ', ',')); 
                        last = current;
                    }
                    break;
                default:
                    break;
            }
            current++;
        }
    
        if (last != csvText.Length - 1) 
        {
            tokens.Add(csvText.Substring(last+1).Trim());
        }
    
        return tokens.ToArray();
    }
    

提交回复
热议问题