我需要通过“逗号分隔符”分割文本 ...和“字符串标识符”
输入 “dtl.txt”
AWD_CODE,AWD_NAME,AWD_TYPE,ADF_REF,FLG_SUM,FLG
DMM,PETCH,01,REF 2/2015,,
TRR,TUCTH,01,REF 2/2015,WD_TRK,F
TGC,DHYTH,02,REF 3/2015,"WD_TRK,WD_TRI",F
操作
static void Main(string[] args)
{
string[] lines = System.IO.File.ReadAllLines(@"D://dtl.txt");
List<string[]> param = new List<string[]>();
foreach(string line in lines)
{
param.Add(line.Split(','));
}
var x = param; // for debug
}
输出 (获取)
array :
[0] : "AWD_CODE","AWD_NAME","AWD_TYPE","ADF_REF","FLG_SUM","FLG"
[1] : "DMM","PETCH","01","REF 2/2015","",""
[2] : "TRR","TUCTH","01","REF 2/2015","WD_TRK","F"
[3] : "TGC","DHYTH","02","REF 3/2015","\"WD_TRK","WD_TRI\"","F"
输出 (需要)
array :
[0] : "AWD_CODE","AWD_NAME","AWD_TYPE","ADF_REF","FLG_SUM","FLG"
[1] : "DMM","PETCH","01","REF 2/2015","",""
[2] : "TRR","TUCTH","01","REF 2/2015","WD_TRK","F"
[3] : "TGC","DHYTH","02","REF 3/2015","WD_TRK,WD_TRI","F"
“WD_TRK,WD_TRI”是的,代码也将其拆分。
但我不需要,任何人都可以帮助解决这个问题吗?
答案 0 :(得分:1)
这是TextFieldParser
库中Microsoft.VisualBasic.FileIO
最适合的情况。
using Microsoft.VisualBasic.FileIO; //add this
static void Main(string[] args)
{
string text = System.IO.File.ReadAllText(@"D://dtl.txt"); //note this
List<string[]> param = new List<string[]>();
string[] words; //add intermediary reference
using (TextFieldParser parser = new TextFieldParser(new StringReader(text))) {
parser.Delimiters = new string[] { "," }; //the parameter must be comma
parser.HasFieldsEnclosedInQuotes = true;
while ((words = parser.ReadFields()) != null)
param.Add(words);
}
var x = param; // for debug
}
你将得到你需要的东西。阅读this。
输出:
array :
[0] : "AWD_CODE","AWD_NAME","AWD_TYPE","ADF_REF","FLG_SUM","FLG"
[1] : "DMM","PETCH","01","REF 2/2015","",""
[2] : "TRR","TUCTH","01","REF 2/2015","WD_TRK","F"
[3] : "TGC","DHYTH","02","REF 3/2015","WD_TRK,WD_TRI","F"
要使用它,您需要在参考中加入Microsoft.VisualBasic
。
答案 1 :(得分:0)
除非您在此特定情况下使用专门的CSV库(强烈推荐),否则您需要编写正则表达式。有关类似问题,请参阅C#, regular expressions : how to parse comma-separated values, where some values might be quoted strings themselves containing commas。给出的正则表达式是
"[^"\r\n]*"|'[^'\r\n]*'|[^,\r\n]*
使用此代码执行它:
Regex regexObj = new Regex(@"""[^""\r\n]*""|'[^'\r\n]*'|[^,\r\n]*");
Match matchResults = regexObj.Match(input);
while (matchResults.Success)
{
Console.WriteLine(matchResults.Value);
matchResults = matchResults.NextMatch();
}