7

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.

4
  • 1
    How would it be possible to decern from comma's that are in the data and comma's that separate the data? Commented Sep 16, 2008 at 15:16
  • Because this question will eventually be indexed by google for those keywords. It would be nice to have a more complete answer near the top. Commented Sep 16, 2008 at 19:25
  • I think that if you wanted to ask a different question (or the same one with more details), you should have done that, instead of hijacking this one and making it look like answers that solvers the original asker's question were wrong. Commented Sep 17, 2008 at 5:58
  • It is true that I can guarantee there are no commas in the data so the acceptable answer for my question is the one I accepted, string[] splitString = origString.Split(','); I can see the value of the regex approach if there is no guarantee though... Commented Sep 17, 2008 at 9:03

17 Answers 17

17

String.Split is just not going to cut it, but a Regex.Split may - Try this one:

using System.Text.RegularExpressions;

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

Where 'input' is the csv line. This will handle quoted delimiters, and should give you back an array of strings representing each field in the line.

Sign up to request clarification or add additional context in comments.

3 Comments

For the records, this regex works great on the data I'm using, which has quoted data with commas.
This doesn't work right for non-quoted data (if you have it): e.g. it fails on the following: String s = "a,b,c";
would this work for a csv file with multiple lines/records?
8

If you want robust CSV handling, check out FileHelpers

1 Comment

FileHelpers is a great library for delimited and fixed length records. Consuming CSV files is a breeze.
2
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.

5 Comments

Someone with edit privileges please fix the code in this post.
If you're referring to wanting .Split({','}), in C# and .NET 2.0 this is not necessary. The .Split(..) overload I'm using takes a params char[], the compiler makes it into an array.
How can this be the answer.... since the question clearly states that splitting on a comma isn't good enough?
It may not be clear in the question, but splitting on a comma is good enough as I can guarantee no comma's in the data
The bolded bit in the question was not added by the original poster and adds unnecessary requirements to his questions that he doesn't need. Allowing users to change the question is a huge problem I think Stack Overflow needs to address.
2

Try:

Regex rex = new Regex(",(?=([^\"]*\"[^\"]*\")*(?![^\"]*\"))");
string[] values = rex.Split( csvLine );

Source: http://weblogs.asp.net/prieck/archive/2004/01/16/59457.aspx

Comments

2

You can take a look at using the Microsoft.VisualBasic assembly with the

Microsoft.VisualBasic.FileIO.TextFieldParser

It handles CSV (or any delimiter) with quotes. I've found it quite handy recently.

Comments

1

There isn't a simple way to do this well, if you want to account for quoted elements with embedded commas, especially if they are mixed with non-quoted fields.

You will also probably want to convert the lines to a dictionary, keyed by the column name.

My code to do this is several hundred lines long.

I think there are some examples on the web, open source projects, etc.

Comments

1

Try this;

static IEnumerable<string> CsvParse(string input)
{
    // null strings return a one-element enumeration containing null.
    if (input == null)
    {
        yield return null;
        yield break;
    }

    // we will 'eat' bits of the string until it's gone.
    String remaining = input;
    while (remaining.Length > 0)
    {

        if (remaining.StartsWith("\"")) // deal with quotes
        {
            remaining = remaining.Substring(1); // pass over the initial quote.

            // find the end quote.
            int endQuotePosition = remaining.IndexOf("\"");
            switch (endQuotePosition)
            {
                case -1:
                    // unclosed quote.
                    throw new ArgumentOutOfRangeException("Unclosed quote");
                case 0:
                    // the empty quote
                    yield return "";
                    remaining = remaining.Substring(2);
                    break;
                default:
                    string quote = remaining.Substring(0, endQuotePosition).Trim();
                    remaining = remaining.Substring(endQuotePosition + 1);
                    yield return quote;
                    break;
            }
        }
        else // deal with commas
        {
            int nextComma = remaining.IndexOf(",");
            switch (nextComma)
            {
                case -1:
                    // no more commas -- read to end
                    yield return remaining.Trim();
                    yield break;

                case 0:
                    // the empty cell
                    yield return "";
                    remaining = remaining.Substring(1);
                    break;

                default:
                    // get everything until next comma
                    string cell = remaining.Substring(0, nextComma).Trim();
                    remaining = remaining.Substring(nextComma + 1);
                    yield return cell;
                    break;
            }
        }
    }

}

Comments

0
CsvString.split(',');

2 Comments

And if there are commas in the data?
then String.split(", "); and if the data contains commas with spaces then csv was a poor choice for storing and transmitting the data
0

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({','}};
}

2 Comments

I think we are looking for a single line, csv to value sets array
Added that...I thought the question was ambiguous.
0
string s = "1,2,3,4,5";

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

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

1 Comment

it takes a params char[], which means you don't need to create the char array, the compiler will do that for you
0

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

1 Comment

For an example of this, if you have Excel, create a new document and enter data into a field with commas. Save as a CSV file, then open it up in a text editor. Enlightenment for mmattax.
0

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.

1 Comment

"A CSV file with quoted fields, is not a CSV file." == false en.wikipedia.org/wiki/Comma-separated_values
0

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.

Comments

0

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();
}

Comments

0
string test = "one,two,three";
string[] okNow = test.Split(',');

Comments

0
separationChar[] = {';'}; // or '\t' ',' etc.
var strArray = strCSV.Split(separationChar);

Comments

0
string[] splitStrings = myCsv.Split(",".ToCharArray());

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.