-1

文本文件:

$3.00,0.00,0.00,1.00,L55894M8,$3.00,0.00,0.00,2.00,L55894M9

如何拆分线路并获得和之类的序列L55894M8L55894M9

4

5 回答 5

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() });
于 2011-12-16T03:43:37.553 回答
0

如果文件在字符串中,您可以使用字符串的.split(',')方法,然后检查结果数组的每个元素。或者如果在整个过程中都可以看到这种数据模式,则每 5 个元素就抓取一次。

于 2011-12-16T03:41:07.577 回答
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"
于 2011-12-16T03:43:21.840 回答
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);
于 2011-12-16T03:43:54.237 回答
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();
    }
于 2011-12-16T07:17:06.213 回答