asp.net Convert CSV string to string[]

0 votes
asked Sep 16, 2008 by col

Is there an easy way to convert a string from csv format into a string[] or list?

I can guarantee that there are no commas in the data.

16 Answers

0 votes
answered Jan 16, 2008 by john-sheehan-runscop

Get a string[] of all the lines:

string[] lines = System.IO.File.ReadAllLines("yourfile.csv");

Then loop through and split those lines (this error prone because it doesn't check for commas in quote-delimited fields):

foreach (string line in lines)
{
    string[] items = line.Split({','}};
}
0 votes
answered Jan 16, 2008 by paul-van-brenk
separationChar[] = {';'}; // or '\t' ',' etc.
var strArray = strCSV.Split(separationChar);
0 votes
answered Jan 16, 2008 by bob-king
string s = "1,2,3,4,5";

string myStrings[] = s.Split({','}};

Note that Split() takes an array of characters to split on.

0 votes
answered Jan 16, 2008 by ryan-steckler
string[] splitStrings = myCsv.Split(",".ToCharArray());
0 votes
answered Jan 16, 2008 by sam

Some CSV files have double quotes around the values along with a comma. Therefore sometimes you can split on this string literal: ","

0 votes
answered Jan 16, 2008 by claco

A Csv file with Quoted fields, is not a Csv file. Far more things (Excel) output without quotes rather than with quotes when you select "Csv" in a save as.

If you want one you can use, free, or commit to, here's mine that also does IDataReader/Record. It also uses DataTable to define/convert/enforce columns and DbNull.

http://github.com/claco/csvdatareader/

It doesn't do quotes.. yet. I just tossed it together a few days ago to scratch an itch.

Forgotten Semicolon: Nice link. Thanks. cfeduke: Thanks for the tip to Microsoft.VisualBasic.FileIO.TextFieldParser. Going into CsvDataReader tonight.

0 votes
answered Jan 17, 2008 by claco

http://github.com/claco/csvdatareader/ updated using TextFieldParser suggested by cfeduke.

Just a few props away from exposing separators/trimspaces/type ig you just need code to steal.

0 votes
answered Jan 17, 2008 by johnopincar

I was already splitting on tabs so this did the trick for me:

public static string CsvToTabDelimited(string line) {
    var ret = new StringBuilder(line.Length);
    bool inQuotes = false;
    for (int idx = 0; idx < line.Length; idx++) {
        if (line[idx] == '"') {
            inQuotes = !inQuotes;
        } else {
            if (line[idx] == ',') {
                ret.Append(inQuotes ? ',' : '\t');
            } else {
                ret.Append(line[idx]);
            }
        }
    }
    return ret.ToString();
}
0 votes
answered Sep 16, 2008 by developingchris
CsvString.split(',');
0 votes
answered Sep 16, 2008 by timothy-carter
string[] splitString = origString.Split(',');

(Following comment not added by original answerer) Please keep in mind that this answer addresses the SPECIFIC case where there are guaranteed to be NO commas in the data.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...