Splitting string on commas when data can contain commas

前端 未结 3 839
小蘑菇
小蘑菇 2021-01-13 04:51

I have a CSV file (which I didn\'t design and I can\'t change now nor will I ever be able to change it) that contains lines like the following:

\"Surname, Firs

相关标签:
3条回答
  • 2021-01-13 05:39

    I know there's a lot of people here who think character-by-character comparisons should never be used and will strongly disagree with me but I'm not convinced companies like Microsoft aren't the only ones who should be doing that sort of programming.

    Afterall, Split does character-by-character comparisons so why is it any less ugly when you call existing code that doesn't quite do exactly what you want?

    At any rate, my approach was to write my own code. And I've posted the code online at http://www.blackbeltcoder.com/Articles/files/reading-and-writing-csv-files-in-c.

    0 讨论(0)
  • 2021-01-13 05:42

    You can handle this easily by using the TextFieldParser class. Just set HasFieldsEnclosedInQuotes to true.

    0 讨论(0)
  • 2021-01-13 05:45

    I would suggest using a CSV parser library - there are other cases that you wouldn't have thought of (new line as part of a quoted field).

    The VisualBasic namespace has a nice library that can help - the TextFieldParser.

    0 讨论(0)
提交回复
热议问题