Reputation: 687
I have written a desktop app to help some coworkers process some huge .csv files they have. Each "column" within a line (row) is in quotation marks, so it looks something like this:
"something", "blah-blah", "another thing", "etc and so forth"
My simple little program reads a line, uses String.Split(',') function to get an array of values, and off I go to do my processing...UNTIL I hit a row like this:
"something", "blah-blah", "Values, 1, 2, 3", "etc and so forth"
The commas within the quoted value make the Split function behave in an unintended way.
Is there an "easy" (built-in) way I can handle inputting the lines that will correctly parse the example above? I want to avoid having to write my own logic to trudge through each line.
I suspect that using Regular Expressions may be the key to happiness.
Thanks, in advance, for any help you can provide.
Upvotes: 0
Views: 207
Reputation: 20777
There are a lot of edge cases when dealing with quoted strings in CSV and commas/quotes within them. I'd recommend using a library like CsvHelper (or one of the others available in NuGet) that have already figured out the logic and tested it.
Other options:
Upvotes: 2
Reputation: 25370
You can trim the first and last quotation off, then it'd look like ->
something", "blah-blah", "Values, 1, 2, 3", "etc and so forth
then you can split on ", " like
String.Split(@""", """);
or do the split first, then .Replace(@"""", "");
Upvotes: 0