tags:

views:

175

answers:

5

Hi ! 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

+6  A: 

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

FileHelpers is a nice library for this job.

Darin Dimitrov
Its actually a pretty simple finite state machine to make. I wrote one a few years back because ADO felt like an overkill for this task.
liho1eye
+1. http://www.codeproject.com/KB/database/CsvReader.aspx is a nice lightweight one. @liho1eye: it may be simple, but reinventing the wheel is not creating value for your customer.
TrueWill
@TrueWill thats kinda circular logic. Besides I just looked at your link and that seems almost identical to my solution... possibly much better polished, but (looking at revision log) younger than mine by at least 2 years. Not that I am trying to claim rights to this implementation. Its just goes to prove that CSV parser is pretty easy to make.
liho1eye
@liho1eye: If you have an alternate implementation, great! Post it as open source (if you own the copyright) and let others benefit from your efforts. What I'm saying is that there is no reason for anyone else to write a CSV parser other than (a) as a learning exercise, (b) none is available on their platform, or (c) existing parsers do not meet their business requirements.
TrueWill
+1  A: 

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.

mehmet6parmak
That could be great.. the problem is that the csv file is not valid ! .. thanks to the file creator..
PaN1C_Showt1Me
@PaN1C_Showt1Me: The example CSV is valid. The field containing a comma is enclosed in double-quotes. See http://en.wikipedia.org/wiki/Comma-separated_values
TrueWill
A: 

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.

Ash Burlaczenko
except you going to split on all the commas inside values to
liho1eye
yep.. that's the problem..
PaN1C_Showt1Me
+2  A: 

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.

Guffa
A: 

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, ",");