Find specific value within a .txt file C #

3

I have a .txt file with the following lines:

000-000 = CRT
001-000 = 00000021
009-000 = 00
012-000 = 247823
013-000 = 0000559877
022-000 = 24082017
023-000 = 152842
032-000 = 80F1
100-000 = JORGE EXPEDITO      
307-000 = S17SNSNNNSPSS9MSNN00
308-000 = CLIENTE TESTE - APENAS MEDICAMENTOS. DEVE SER APRESENTADA RECEITA 
MEDICA EM MESMO NOME DO CARTAO. PODE HAVER COBRANCA DE VALOR A VISTA.
370-000 = CONSULT CLIENTE-CARTAO   
999-999 = 

I need to get the value of parameter 001-000 = ... that is inside the file. Any idea how to do this in C # I thought of something with LINQ but I do not know how to implement, any ideas?

    
asked by anonymous 25.08.2017 / 14:01

2 answers

4

Basically, this is it:

var valores = File.ReadAllLines("NomeDoArquivo.txt")
                  .Where(l => l.StartsWith("001-000"))
                  .Select(l => l.Substring(l.LastIndexOf("=") + 1))
                  .ToList();

The use would be

foreach(var valor in results)
    Console.WriteLine(valor);

File.ReadAllLines returns an array of string with all lines of the file.

The Where is the filter, it says: "For each line of the file, return only those that start with 001-000 .

The Select will be on the collection produced by Where , that is, only lines that start with 001-000 . And for item in this collection, its substring will be selected starting from the index of the equality symbol ( = ), which is its key separator.

This will produce a list of results, if you have absolute certainty that there will always be a space between the equal sign and the value, you can change +1 of Select by +2 .

    
25.08.2017 / 14:12
1

You can keep a List

25.08.2017 / 14:22