Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

csv parse issue with commas

Status
Not open for further replies.

darylbewise

Programmer
Jan 5, 2009
16
0
0
GB
Hi all,

I have built a CSV parser that will convert each line of a csv cod into a string[].

Code:
if (File.Exists(path))
{
            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    while (!sr.EndOfStream)
                    {
                        string[] y = sr.ReadLine().Split(',');
                    }
                }
            }
}

This works for the majority of csv documents. However when a column within the CSV contains a comma, the split will not split the columns out corrently.

For Example:

csv file:
1, 2, 3, "4,5"

output I want:
y[0] = 1
y[1] = 2
y[2] = 3
y[3] = "4,5"

output i get:
y[0] = 1
y[1] = 2
y[2] = 3
y[3] = "4
y[4] = 5"

Any ideas how I can get this issue resolved?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top