Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a CSV and excluding commas within elements

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 commas.

For example, I might have a CSV as follows:

1,"Hello",2,"World",3,"Hello, World"

I would like it so the string is split into:

1
"Hello"
2
"World"
3
"Hello, World"

If I use String.Split(','); I get:

1
"Hello"
2
"World"
3
"Hello
World"

Is there an easy way of doing this? A library that is already written or do I have to parse the string character by character?

like image 979
lancscoder Avatar asked Sep 17 '25 04:09

lancscoder


1 Answers

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

like image 107
Marc Gravell Avatar answered Sep 19 '25 18:09

Marc Gravell