-1

Text File:

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

How do I split the line and get the serial number like L55894M8 and L55894M9?

dbc
  • 104,963
  • 20
  • 228
  • 340
user1101157
  • 79
  • 1
  • 1
  • 7

5 Answers5

1

To get the data that appears after the 4th comma and 9th comma, you would want to do:

var pieces = line.Split(',');
var serial1 = line[3];
var serial2 = line[8];

Edit: Upon further reflection, it appears your file has records that begin with $ and end with the next record. If you want these records, along with the serial number (which appears to be the last field) you can do:

var records = line.TrimStart('$').Split('$');
var recordObjects = records.Select(r => new { Line = r, Serial = r.TrimEnd(',').Split(',').Last() });
David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
0

if the file is in a string you can use the string's .split(',') method then check each element of the resulting array. Or grab every 5th element if that pattern of data is seen throughout.

smitec
  • 3,049
  • 1
  • 16
  • 12
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"
Chris Fulstow
  • 41,170
  • 10
  • 86
  • 110
0

Try regular expression.

 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);
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

In your sample you means want to get the words in index of ( 4 , 9 , 14 .... )

And the five words as a party .

So you can try this way.....

    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();
    }
shenhengbin
  • 4,236
  • 1
  • 24
  • 33