Question

I am creating a string array of comma separated values. I need to split the line on commas but not commas that are inside quotes as that could be part of the data.

For example:
\"421 15th Ave.\",\"Beaver Falls, PA\",\"S1\",

I wouldn't want it to split between the city and state for a new field.

Here is the code currently:

string[] fields = Regex.Split(lines[i], @",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))");

It takes a long time to execute. Is there a better option I could be using?

Thanks for any help on this.

Was it helpful?

Solution

Have you considered using a CSV-Reader? This CSV-Reader should already handle the case of commas inside double quotes: OpenCsv

See Ravi Thapliyal's answer in this thread about how to use it: link

For C# I can recommend this CsvHelper: https://github.com/JoshClose/CsvHelper

We use it in our project and it's very handy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top