2

So Im reading a csv file and splitting the string with "," as the deliminator

but some of them have quotes as to not split the specific field because it has a comma in it.

1530,Pasadena CA,"2008, 05/01","2005, 12/14"

with just comma it would be:

1530 Pasadena CA "2008 05/01" "2005 12/14"

I need it to take commas into consideration when splitting so its like this

1530 Pasadena CA "2008 05/01" "2005 12/14"

user484616
  • 23
  • 1
  • 3
  • 1
    That's a common problem, solved in many places. Did you search google or CodeProject for a CSV parser written in C#? That would likely answer the question for you. – John Fisher Oct 22 '10 at 19:50
  • possible duplicate of [Parse Delimited CSV in .NET](http://stackoverflow.com/questions/736629/parse-delimited-csv-in-net) – Yuriy Faktorovich Oct 22 '10 at 19:52

2 Answers2

2

Take a look at this page for a library that offers quick and easy CSV reading.

Gabriel McAdams
  • 56,921
  • 12
  • 61
  • 77
  • Trying not to use an external library, I was thinking something like – user484616 Oct 22 '10 at 19:58
  • strLine = strLine.Replace("\\"", "\'\'"); – user484616 Oct 22 '10 at 19:58
  • so replace "" with '' then replace , with " then replace "" with , eliminating the quotes but keeping the commas – user484616 Oct 22 '10 at 19:59
  • 1
    If you just remove the quotes, then you're altering the CSV. You could end up reading it incorrectly. There are a lot of things to be careful with, regarding CSVs. I would look at the library. There's nothing wrong with using it. Its using the MIT license, so its not a problem to include it in your deployment. – Gabriel McAdams Oct 22 '10 at 20:02
  • Thanks for the link, this library works beautifully. No need to reinvent the wheel ;) – Avindra Goolcharan Jan 13 '11 at 12:35
0

While it still may be a new reference, there is a class within the Visual Basic assemblies that should handle this well. At least then you know it's a part of the framework. You can find details here: http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.aspx

Matt Klinker
  • 773
  • 5
  • 18