How to properly split a CSV using C# split() function?

前端 未结 6 875
旧时难觅i
旧时难觅i 2020-12-05 19:21

Suppose I have this CSV file :

NAME,ADDRESS,DATE
\"Eko S. Wibowo\", \"Tamanan, Banguntapan, Bantul, DIY\", \"6/27/1979\"

I would like like

6条回答
  •  感动是毒
    2020-12-05 20:01

    You could use regex too:

    string input = "\"Eko S. Wibowo\", \"Tamanan, Banguntapan, Bantul, DIY\", \"6/27/1979\"";
    string pattern = @"""\s*,\s*""";
    
    // input.Substring(1, input.Length - 2) removes the first and last " from the string
    string[] tokens = System.Text.RegularExpressions.Regex.Split(
        input.Substring(1, input.Length - 2), pattern);
    

    This will give you:

    Eko S. Wibowo
    Tamanan, Banguntapan, Bantul, DIY
    6/27/1979
    

提交回复
热议问题