2008-09-16 70 views
6

是否有一種簡單的方法將字符串從csv格式轉換爲字符串[]或列表?asp.net將CSV字符串轉換爲字符串[]

我可以保證數據中沒有逗號。

+1

它是如何將有可能從逗號的是在數據和逗號的數據分開來decern? – mmattax 2008-09-16 15:16:09

+0

因爲這個問題最終會被谷歌索引爲這些關鍵字。在頂部附近有更完整的答案會很好。 – 2008-09-16 19:25:20

+0

我認爲,如果你想問一個不同的問題(或者具有更多細節的問題),你應該做到這一點,而不是劫持這個問題,並使它看起來像解答原始提問者的問題是錯誤的答案。 – travis 2008-09-17 05:58:08

回答

2
string[] splitString = origString.Split(',');

(以下不是原來的回答者添加評論) 請記住,這個答案解決那裏都保證在數據沒有逗號的特殊情況。

+0

有編輯權限的人請修正本文中的代碼。 – 2008-09-16 15:18:29

+0

如果你指的是想要.Split({','}),在C#和.NET 2.0中,這是沒有必要的。我使用的.Split(..)重載需要一個params char [],編譯器會將它變成一個數組。 – 2008-09-16 15:38:02

+0

這怎麼能成爲答案......因爲這個問題明確指出,在逗號分割不夠好? – 2008-09-17 07:48:35

0
CsvString.split(','); 
+0

如果數據中有逗號? – 2008-09-16 15:13:18

+0

then String.split(「,」);如果數據包含帶空格的逗號,那麼csv是存儲和傳輸數據的不好選擇。 – cazlab 2008-09-16 15:15:51

0

獲得一個字符串[所有線路]:

string[] lines = System.IO.File.ReadAllLines("yourfile.csv"); 

通過然後循環,分裂這些線路(這很容易出錯,因爲它沒有在報價分隔領域逗號檢查):

foreach (string line in lines) 
{ 
    string[] items = line.Split({','}}; 
} 
0
string test = "one,two,three"; 
string[] okNow = test.Split(','); 
0
string s = "1,2,3,4,5"; 

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

注意,分割()取CHARAC的陣列 ters分裂。

0
separationChar[] = {';'}; // or '\t' ',' etc. 
var strArray = strCSV.Split(separationChar); 
0
string[] splitStrings = myCsv.Split(",".ToCharArray()); 
1

沒有一個簡單的方法來做好這些工作,如果你想佔嵌入逗號引述因素,特別是當它們與非引用字段混合在一起。

您也可能想將行轉換爲字典,並以列名爲鍵。

我的代碼要做幾百行。

我覺得有在網絡上的一些例子,開源項目等

0

有些CSV文件以沿着值雙引號用逗號。因此,有時您可以在此字符串文字上進行拆分:「,」

0

帶引號字段的Csv文件不是Csv文件。更多的事情(Excel)輸出時不帶引號,而不是在保存中選擇「Csv」時引用引號。

如果你想要一個你可以使用,免費或承諾,這裏是我的也IDataReader /記錄。它還使用DataTable來定義/轉換/強制列和DbNull。

http://github.com/claco/csvdatareader/

它不會做報價..呢。幾天前我把它扔在一起,以防止發癢。

忘記分號:尼斯鏈接。謝謝。 cfeduke:感謝您向Microsoft.VisualBasic.FileIO.TextFieldParser提示。今晚進入CsvDataReader。

16

String.Split只是不會削減它,但Regex.Split可能 - 試試這個:

using System.Text.RegularExpressions; 

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

其中「輸入」是CSV行。這將處理帶引號的分隔符,並且應該返回表示該行中每個字段的字符串數組。

2

你可以看看使用Microsoft.VisualBasic程序集與

Microsoft.VisualBasic.FileIO.TextFieldParser 

它處理CSV(或任何分隔符)與報價。最近我發現它非常方便。

1

試試這個;

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

} 
0

我已經分裂的標籤所以這爲我做的伎倆:

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