回答
string[] splitString = origString.Split(',');
(以下不是原來的回答者添加評論) 請記住,這個答案解決那裏都保證在數據沒有逗號的特殊情況。
有編輯權限的人請修正本文中的代碼。 – 2008-09-16 15:18:29
如果你指的是想要.Split({','}),在C#和.NET 2.0中,這是沒有必要的。我使用的.Split(..)重載需要一個params char [],編譯器會將它變成一個數組。 – 2008-09-16 15:38:02
這怎麼能成爲答案......因爲這個問題明確指出,在逗號分割不夠好? – 2008-09-17 07:48:35
CsvString.split(',');
如果數據中有逗號? – 2008-09-16 15:13:18
then String.split(「,」);如果數據包含帶空格的逗號,那麼csv是存儲和傳輸數據的不好選擇。 – cazlab 2008-09-16 15:15:51
獲得一個字符串[所有線路]:
string[] lines = System.IO.File.ReadAllLines("yourfile.csv");
通過然後循環,分裂這些線路(這很容易出錯,因爲它沒有在報價分隔領域逗號檢查):
foreach (string line in lines)
{
string[] items = line.Split({','}};
}
string test = "one,two,three";
string[] okNow = test.Split(',');
string s = "1,2,3,4,5";
string myStrings[] = s.Split({','}};
注意,分割()取CHARAC的陣列 ters分裂。
separationChar[] = {';'}; // or '\t' ',' etc.
var strArray = strCSV.Split(separationChar);
string[] splitStrings = myCsv.Split(",".ToCharArray());
如果你想強大的CSV處理,檢查出FileHelpers
沒有一個簡單的方法來做好這些工作,如果你想佔嵌入逗號引述因素,特別是當它們與非引用字段混合在一起。
您也可能想將行轉換爲字典,並以列名爲鍵。
我的代碼要做幾百行。
我覺得有在網絡上的一些例子,開源項目等
嘗試:
Regex rex = new Regex(",(?=([^\"]*\"[^\"]*\")*(?![^\"]*\"))");
string[] values = rex.Split(csvLine);
來源:http://weblogs.asp.net/prieck/archive/2004/01/16/59457.aspx
有些CSV文件以沿着值雙引號用逗號。因此,有時您可以在此字符串文字上進行拆分:「,」
帶引號字段的Csv文件不是Csv文件。更多的事情(Excel)輸出時不帶引號,而不是在保存中選擇「Csv」時引用引號。
如果你想要一個你可以使用,免費或承諾,這裏是我的也IDataReader /記錄。它還使用DataTable來定義/轉換/強制列和DbNull。
http://github.com/claco/csvdatareader/
它不會做報價..呢。幾天前我把它扔在一起,以防止發癢。
忘記分號:尼斯鏈接。謝謝。 cfeduke:感謝您向Microsoft.VisualBasic.FileIO.TextFieldParser提示。今晚進入CsvDataReader。
String.Split只是不會削減它,但Regex.Split可能 - 試試這個:
using System.Text.RegularExpressions;
string[] line;
line = Regex.Split(input, ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
其中「輸入」是CSV行。這將處理帶引號的分隔符,並且應該返回表示該行中每個字段的字符串數組。
你可以看看使用Microsoft.VisualBasic程序集與
Microsoft.VisualBasic.FileIO.TextFieldParser
它處理CSV(或任何分隔符)與報價。最近我發現它非常方便。
試試這個;
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;
}
}
}
}
http://github.com/claco/csvdatareader/使用cfeduke建議的TextFieldParser進行更新。
從暴露分隔符/ trimspaces /類型ig只是一些道具,你只需要竊取代碼。
我已經分裂的標籤所以這爲我做的伎倆:
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();
}
- 1. 將字符串轉換爲字符串
- 2. 將字符串轉換爲字符串
- 3. 將字符串轉換爲字符串
- 4. 將CSV字符串[]轉換爲字符串[] []
- 5. 將XML字符串轉換爲CSV?
- 6. 將csv轉換爲字符串變量
- 7. 將字符串轉換爲int,int轉換爲字符串
- 8. 將字符串轉換爲DateTime Asp.Net
- 9. 將字符串轉換爲表ASP.Net MVC
- 10. Asp.Net:將字符串轉換爲長
- 11. 將ASP.Net GridView HTML轉換爲字符串
- 12. 將字符串轉換爲DateTime C#ASP.NET
- 13. 將字符串轉換爲字符ascii
- 14. Java - 將字符串轉換爲字符[]
- 15. C++ - 將字符串轉換爲字符
- 16. 將字符*轉換爲字符串
- 17. 將字符串轉換爲字符
- 18. 將字符串轉換爲const *字符
- 19. 將字符[]轉換爲字符串
- 20. InfixToPostfix將字符轉換爲字符串
- 21. 將字符串轉換爲字符
- 22. 將char字符串轉換爲字符
- 23. 將字符串轉換爲字符
- 24. 將轉換器映射字符串轉換爲字符串
- 25. 將字符串轉換爲「_」
- 26. 將字符串轉換爲
- 27. 將字符串轉換爲?
- 28. 如何將字符串的字符串轉換爲字符?
- 29. JQuery.each將字符串文字轉換爲字符串。爲什麼?
- 30. 將字符串轉換爲字符串後無法反轉字符串Array
它是如何將有可能從逗號的是在數據和逗號的數據分開來decern? – mmattax 2008-09-16 15:16:09
因爲這個問題最終會被谷歌索引爲這些關鍵字。在頂部附近有更完整的答案會很好。 – 2008-09-16 19:25:20
我認爲,如果你想問一個不同的問題(或者具有更多細節的問題),你應該做到這一點,而不是劫持這個問題,並使它看起來像解答原始提問者的問題是錯誤的答案。 – travis 2008-09-17 05:58:08