asp.net Convert CSV string to string[]

余生长醉 提交于 2020-01-22 19:29:06

问题


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.


回答1:


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.




回答2:


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.




回答3:


If you want robust CSV handling, check out FileHelpers




回答4:


Try:

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

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




回答5:


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.




回答6:


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.




回答7:


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

}



回答8:


CsvString.split(',');



回答9:


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



回答10:


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

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

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




回答11:


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




回答12:


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.




回答13:


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.




回答14:


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



回答15:


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



回答16:


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



回答17:


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


来源:https://stackoverflow.com/questions/73385/asp-net-convert-csv-string-to-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!