Search code examples
c#substringreadlinedata-extractionstartswith

ReadLines to find Selector and then Get Data from the Next Line


I am trying to obtain some data from a PDF but having an issue as the recurring identifier is on the line above the data I need, and for each PDF the index might be different depending on the contents At the point of reading the file, it has been OCR'd from PDF to a Text File. So reading from the Text file.

I am trying to get the Currency; in this case to get "EUR"
Data being read:

Currency Charge Totals
EUR 233.00

var currencyLine = File.ReadLines(extractData).Last(e => e.StartsWith("Currency Charge Totals"));
out_currency = (currencyLine.Substring(currencyLine.LastIndexOf(" ") + 1)).Trim();

The 'Currency Charge Totals' in this PDF to obtain the currency type. I'm aware that currently this code will not return any value at the second, I've just being doing similar to obtain other data.

I just need some assistance on what I need to change to get the details from the Line below the 'ReadLine'


Solution

  • If you need line after the one you are looking for, you can do it like this:

    File
    .ReadLines(extractData)
    .SkipWhile(e => !e.StartsWith("Currency Charge Totals"))
    .Skip(1)
    .FirstOrDefault();