Split String in C#

前端 未结 9 986
萌比男神i
萌比男神i 2020-12-28 09:10

I thought this will be trivial but I can\'t get this to work.

Assume a line in a CSV file: \"Barack Obama\", 48, \"President\", \"1600 Penn Ave, Washington DC

9条回答
  •  北海茫月
    2020-12-28 09:44

    You can't parse a CSV line with a simple Split on commas, because some cell contents will contain commas that aren't meant to delineate data but are actually part of the cell contents themselves.

    Here is a link to a simple regex-based C# method that will convert your CSV into a handly DataTable:

    http://www.hotblue.com/article0000.aspx?a=0006

    Working with DataTables is very easy - let me know if you need a code sample for that.

提交回复
热议问题