Search specific value within a file . txt C#

Asked

Viewed 1,359 times

4

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 take 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 don’t know how to implement, some idea?

  • on the line 308-000 has line break or is just a line ?

  • is just one line!

  • What have you done so far? Share the code.

  • You can do a file read and check if the line contains what you’re looking for... either way you’ll need to upload the file contents to C#

  • yes the point is that I have to check the value after the equal sign = of parameter 001-000

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 a string array with all rows of the file.

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

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

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

  • As I said, Sidnei, it produces a list with the results. Try to capture the 0 position of your result (valor[0]).

  • I made it quick and had the return displayed but displayed this message on the console: System.Linq.Enumerable+Whereselectarrayiterator`2[System.String,System.String]

  • Obviously it is possible to adapt the code to produce only one result, but as the expense is the same, I left returning a list

  • Opa I got here. Ptz thank you very much!!!

1

You can keep a List<> with all the parameters, and when you need to, fetch the necessary value. I made an example:

Class Parametro:

public class Parametro
{
    public string Id { get; set; }
    public string Valor { get; set; }

    public static List<Parametro> CarregarParametros()
    {
        List<Parametro> lista = new List<Parametro>();

        TextReader tr = new StreamReader("C:\\parametros.txt", Encoding.Default);
        string linha = "";
        while ((linha = tr.ReadLine()) != null)
        {
            if (!linha.StartsWith("*")) //parâmetro comentado ?!
            {
                string[] value = linha.Split('=');
                lista.Add(new Parametro() { Id = value[0].Trim(), Valor = value[1].Trim() });
            }
        }

        return lista;
    }

}

Loading the list and searching for a value:

    public Form1()
    {
        InitializeComponent();

        List<Parametro> parametros = Parametro.CarregarParametros();

        string valor001 = parametros.Single(p => p.Id == "001-000").Valor;
    }
  • 1

    Opa this is a good idea also I was thinking of something generic as well. It helps me a lot too. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.