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
|
|||||||||||||
|
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. |
||||||||||||||
|
6
|
String.Split is just not going to cut it, but a Regex.Split may - Try this one:
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. |
||
5
|
If you want robust CSV handling, check out FileHelpers |
||
|
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. |
||
|
1
|
Try:
Source: http://weblogs.asp.net/prieck/archive/2004/01/16/59457.aspx |
||
|
1
|
You can take a look at using the Microsoft.VisualBasic assembly with the
It handles CSV (or any delimiter) with quotes. I've found it quite handy recently. |
||
|
1
|
Try this;
|
||
|
0
|
|
||||
|
0
|
Get a string[] of all the lines:
Then loop through and split those lines (this error prone because it doesn't check for commas in quote-delimited fields):
|
||||
|
0
|
string test = "one,two,three"; string[] okNow = test.Split(','); |
||
|
0
|
Note that Split() takes an array of characters to split on. |
||
|
0
|
separationChar[] = {';'}; // or '\t' ',' etc. var strArray = strCSV.Split(separationChar); |
||
|
0
|
string[] splitStrings = myCsv.Split(",".ToCharArray()); |
||
|
0
|
Some CSV files have double quotes around the values along with a comma. Therefore sometimes you can split on this string literal: "," |
||
|
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. |
||||
|
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. |
||
|
0
|
I was already splitting on tabs so this did the trick for me:
|
||
|