4
1

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.

flag
1  
How would it be possible to decern from comma's that are in the data and comma's that separate the data? – mmattax Sep 16 '08 at 15:16
Not sure how the question can be edited to include information that the original asker might not have needed. From the original posters accepting of the answer below obviously commas in the data was not a concern. – RedWolves Sep 16 '08 at 15:59
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. – Joel Coehoorn Sep 16 '08 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. – travis Sep 17 '08 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... – Col Sep 17 '08 at 9:03

17 Answers

0

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.

link|flag
Someone with edit privileges please fix the code in this post. – John Sheehan Sep 16 '08 at 15:18
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 Sep 16 '08 at 15:38
How can this be the answer.... since the question clearly states that splitting on a comma isn't good enough? – pb Sep 17 '08 at 7:48
1  
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 Sep 17 '08 at 9:05
1  
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 Sep 17 '08 at 13:43
show 1 more comment
6

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.

link|flag
For the records, this regex works great on the data I'm using, which has quoted data with commas. – BenB Feb 18 '09 at 0:53
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 Dec 29 '09 at 19:26
5

If you want robust CSV handling, check out FileHelpers

link|flag
FileHelpers is a great library for delimited and fixed length records. Consuming CSV files is a breeze. – Forgotten Semicolon Sep 16 '08 at 15:20
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.

link|flag
1

Try:

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

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

link|flag
1

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.

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

}
link|flag
0
CsvString.split(',');
link|flag
And if there are commas in the data? – Joel Coehoorn Sep 16 '08 at 15:13
then String.split(", "); and if the data contains commas with spaces then csv was a poor choice for storing and transmitting the data – cazlab Sep 16 '08 at 15:15
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({','}};
}
link|flag
I think we are looking for a single line, csv to value sets array – DevelopingChris Sep 16 '08 at 15:13
Added that...I thought the question was ambiguous. – John Sheehan Sep 16 '08 at 15:15
0

string test = "one,two,three";

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

link|flag
0
string s = "1,2,3,4,5";

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

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

link|flag
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 Sep 16 '08 at 15:24
0

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

var strArray = strCSV.Split(separationChar);

link|flag
0

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

link|flag
0

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

link|flag
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 Sep 16 '08 at 15:29
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.

link|flag
1  
"A CSV file with quoted fields, is not a CSV file." == false en.wikipedia.org/wiki/Comma-separated_values – Forgotten Semicolon Sep 16 '08 at 15:36
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.

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

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.