文字档案:
$3.00,0.00,0.00,1.00,L55894M8,$3.00,0.00,0.00,2.00,L55894M9
如何拆分该行并获取L55894M8
和L55894M9
等序列号?
答案 0 :(得分:1)
要获取第4个逗号和第9个逗号后出现的数据,您可以这样做:
var pieces = line.Split(',');
var serial1 = line[3];
var serial2 = line[8];
编辑:进一步反映后,您的文件似乎有以$开头的记录,并以下一条记录结束。如果您想要这些记录以及序列号(看起来是最后一个字段),您可以这样做:
var records = line.TrimStart('$').Split('$');
var recordObjects = records.Select(r => new { Line = r, Serial = r.TrimEnd(',').Split(',').Last() });
答案 1 :(得分:0)
如果文件在字符串中,您可以使用字符串的.split(',')
方法,然后检查结果数组的每个元素。或者,如果始终看到这种数据模式,则抓住每个第5个元素。
答案 2 :(得分:0)
var str = "$3.00,0.00,0.00,1.00,L55894M8,$3.00,0.00,0.00,2.00,L55894M9";
var fieldArray = str.Split(new[] { ',' });
var serial1 = fieldArray[4]; // "L55894M8"
var serial2 = fieldArray[9]; // "L55894M9"
答案 3 :(得分:0)
尝试正则表达式。
string str = "$3.00,0.00,0.00,1.00,L55894M8,$3.00,0.00,0.00,2.00,L55894M9";
string pat = @"L[\w]+";
MatchCollection ar= Regex.Matches(str, pat);
foreach (var t in ar)
Console.WriteLine(t);
答案 4 :(得分:0)
在你的样本中,你的意思是希望得到索引为(4,9,14 ......)
的单词这五个字作为一个聚会。
所以你可以这样试试......
static void Main(string[] args)
{
string strSample = "$3.00,0.00,0.00,1.00,L55894M8,$3.00,0.00,0.00,2.00,L55894M9";
var result = from p in strSample.Split(',').Select((v, i) => new { Index = i, Value = v })
where p.Index % 5 == 4
select p.Value;
foreach (var r in result)
{
Console.WriteLine(r);
}
Console.ReadKey();
}