-3
NS01 EW24
$1.04
$1.32
20
NS04 EW21
$1.02
$1.62
9

where NSxx and EWxx is always 4 characters, the money is always 3 digit with a dollar sign, and the last number can be 1 or 2 digits.

Question: How do I sort NSxx into 1 list, EWxx into another list, first money into another list, second money into another list, and the last number into another list?

(in the text file, which is realy long, there are also other NSxx and EWxx which are CCxx, CGxx, DTxx, NExx)

using (StreamReader fare = new StreamReader("fare.txt"))
{
    string line;
    while ((line = fare.ReadLine()) != null)
    {

    }
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
hakambing
  • 5
  • 2
  • I would look into regular expressions https://www.regextester.com/. At this website you can put some sample data in the source file and start querying against it until you find a good solution. – jwize Feb 02 '19 at 18:34
  • Related: [How to parse a text file with alternating lines of names and lists of integers?](https://stackoverflow.com/q/49341548/3744182). – dbc Feb 02 '19 at 20:06

1 Answers1

1

I would not use different lists but instead create a class able to store one data record.

public class Data
{
    public string Code1 { get; set; }
    public string Code2 { get; set; }
    public decimal Price1 { get; set; }
    public decimal Price2 { get; set; }
    public int Number { get; set; }
}

But use better names. (I don't know what kind of data this is.)

And assuming that the file always contains chunks of 4 lines representing one data record

var fare = File.ReadLines("fare.txt").GetEnumerator();
var list = new List<Data>();
while (fare.MoveNext()) {
    if (!String.IsNullOrEmpty(fare.Current)) { // Not an empty line at the end of the file.
        var data = new Data();
        data.Code1 = fare.Current.Substring(1, 3);
        data.Code2 = fare.Current.Substring(5, 3);
        fare.MoveNext();
        data.Price1 = Decimal.Parse(fare.Current.Substring(1)); // Skip the $ sign.
        fare.MoveNext();
        data.Price2 = Decimal.Parse(fare.Current.Substring(1));
        fare.MoveNext();
        data.Number = Int32.Parse(fare.Current);
        list.Add(data);
    }
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188