tags:

views:

2760

answers:

17

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.

A: 
CsvString.split(',');
DevelopingChris
And if there are commas in the data?
Joel Coehoorn
then String.split(", "); and if the data contains commas with spaces then csv was a poor choice for storing and transmitting the data
cazlab
A: 

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.

Timothy Carter
Someone with edit privileges please fix the code in this post.
John Sheehan
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.
Timothy Carter
How can this be the answer.... since the question clearly states that splitting on a comma isn't good enough?
pb
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
Col
Please edit the question to indicate that...
pb
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.
RedWolves
A: 

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({','}};
}
John Sheehan
I think we are looking for a single line, csv to value sets array
DevelopingChris
Added that...I thought the question was ambiguous.
John Sheehan
A: 

string test = "one,two,three";

string[] okNow = test.Split(',');

A: 
string s = "1,2,3,4,5";

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

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

Bob King
it takes a params char[], which means you don't need to create the char array, the compiler will do that for you
Timothy Carter
A: 

separationChar[] = {';'}; // or '\t' ',' etc.

var strArray = strCSV.Split(separationChar);

pb
A: 

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

Ryan Steckler
+7  A: 

If you want robust CSV handling, check out FileHelpers

John Sheehan
FileHelpers is a great library for delimited and fixed length records. Consuming CSV files is a breeze.
Forgotten Semicolon
+1  A: 

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.

Eric Z Beard
+1  A: 

Try:

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

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

Panos
A: 

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

Sam
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.
Forgotten Semicolon
A: 

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.

claco
"A CSV file with quoted fields, is not a CSV file." == falsehttp://en.wikipedia.org/wiki/Comma-separated_values
Forgotten Semicolon
+8  A: 

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.

Philip Rieck
For the records, this regex works great on the data I'm using, which has quoted data with commas.
BenB
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";
Goyuix
+2  A: 

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.

cfeduke
+1  A: 

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

}
Steve Cooper
A: 

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.

claco
A: 

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