theSpyCry
theSpyCry

Reputation: 12283

CSV row split into string array question

How would you split this row to a string array?

the problem is Rutois, a.s. , so you cannot directly split with ',' separator..

543472,"36743721","Rutois, a.s.","151","some name","01341",55,"112",1

thanks

Upvotes: 1

Views: 6386

Answers (6)

Ryan Loggerythm
Ryan Loggerythm

Reputation: 3314

The other RegEx answer will fail if the first character is a quote.

This is the correct regular expression:

string[] columns = Regex.Split(inputRow, ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");

Upvotes: 0

user427261
user427261

Reputation: 11

I'd be tempted to swap out the quotes that occur inside the quoted strings and then use split. this would work.

        string csv = "543472,\"36743721\",\"Rutois, a.s.\",\"151\",\"some name\",\"01341\",55,\"112\",1"; 


        const string COMMA_TOKEN = "[COMMA]";
        string[] values;
        bool inQuotes = false;

        StringBuilder cleanedCsv = new StringBuilder();
        foreach (char c in csv)
        {
            if (c == '\"')
                inQuotes = !inQuotes;  //work out if inside a quoted string or not
            else
            {
                //Replace commas in quotes with a token
                if (inQuotes && c == ',')
                    cleanedCsv.Append(COMMA_TOKEN);
                else
                    cleanedCsv.Append(c);
            }
        }

        values = cleanedCsv.ToString().Split(',');

        //Put the commas back
        for (int i = 0; i < values.Length; i++)
            values[i] = values[i].Replace(COMMA_TOKEN, ",");

Upvotes: 1

Guffa
Guffa

Reputation: 700252

You can use a regular expression to pick out the values from the line:

string line ="543472,\"36743721\",\"Rutois, a.s.\",\"151\",\"some name\",\"01341\",55,\"112\",1";
var values = Regex.Matches(line, "(?:\"(?<m>[^\"]*)\")|(?<m>[^,]+)");
foreach (Match value in values) {
  Console.WriteLine(value.Groups["m"].Value);
}

Output:

543472
36743721
Rutois, a.s.
151
some name
01341
55
112
1

This of course assumes that you actually have got the complete CSV record in the string. Note that values in a CSV record can contain line breaks, so getting the records from a CSV file can not be done by simply splitting it on line breaks.

Upvotes: 6

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25435

I'm guess you want something like this -

string csv = 543472,"36743721","Rutois, a.s.","151","some name","01341",55,"112",1 ;
string[] values;
values = csv.Split(",");
for(int i = 0; i<values.Length; i++)
{
    values[i] = values[i].Replace("\"", "");
}

Hope this helps.

Upvotes: 0

mehmet6parmak
mehmet6parmak

Reputation: 4857

you can connect to file using odbc check this

link (If link does not help much just google it "connecting csv files with odbc")

If you have problems in odbc also i guess the file is not a valid csv file.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

I would recommend you using a CSV parser instead of rolling your own.

FileHelpers is a nice library for this job.

Upvotes: 7

Related Questions