试试这样的:
string s = "3,dac,fsdfsdf,DdsA 102-13,62.560000000000002,1397,bes,165/70/R13,945,1380,Break,10"
string[] words = s.Split(',');
foreach (string word in words)
{
Console.WriteLine(word);
}
string.Split() 方法会返回一个字符串数组,通过参数你可以指定分割原始字符串的字符。
更新:
好的,请看这个answer。
从那里稍微修改了代码:
public IEnumerable<string> SplitCSV(string input)
{
Regex csvSplit = new Regex("(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)", RegexOptions.Compiled);
foreach (Match match in csvSplit.Matches(input))
{
yield return match.Value.TrimStart(',');
}
}
string s = "3,\"dac\",\"fsdf,sdf\",\"DdsA 102-13\",62.560000000000002,\"1397\",\"bes\",\"165/70/R13\",945,1380,\"Break\",10";
var words = SplitCSV(s);
foreach (string word in words)
{
Console.WriteLine(word);
}
关于如何将此应用于文件中的所有行:
var lines = System.IO.File.ReadAllLines(@"inputfile.txt");
foreach (var line in lines)
{
var wordsInLine = SplitCSV(line);
// do whatever you want with the result...
}