How to change a specific text from a txt file?

Asked

Viewed 249 times

0

In my notepad has 2620 lines which is the maximum.

>PRODUTO [1]
1. Exemplo de texto 1
2. Exemplo de texto 2
>PRODUTO [2]
1. Exemplo de texto 1
2. Exemplo de texto 22
>PRODUTO [3]
1. Exemplo de texto 1
2. Exemplo de texto 2
3. Exemplo de texto 3

@lazyFox: How are you reading the file?

public void Ler_TXT()
{
    string linha = string.Empty;
    List<List<String>> texto = new List<List<String>>();
    StreamReader arquivo = new StreamReader($@"C:\Minha Pasta\MeuTexto.txt", Encoding.UTF8);

    while ((linha = arquivo.ReadLine()) != null)
    {
        if (linha.StartsWith($">PRODUTO"))
        {
            texto.Add(new List<String>());
        }
        else
        {
            texto.Last().Add(linha);
        }
    }
}

It is possible to change a specific text without rewriting the whole text?

You can see on the line : 2. Exemplo de texto 22 is number "2" longer. How can I change specific text. I have the value "PRODUTO [2]" and have value "2.", how can I do this in C# ?

  • How are you reading the file?

  • @lazyFox wait, I will edit question.

  • By the way this file was generated by code put also the "writing"

  • This seems to me very punctual, maybe it would be better to do in Notepad++ with a macro for example. And anyway, I’d have to rewrite the whole file again, just the fact of reading and saving the file would already be rewriting it

  • @lazyFox i no file by logic, is a txt downloaded on the internet (It’s ready).

  • 1

    @Rovannlinhalis 2620 lines is not much for rewriting the whole text?

  • @lazyFox edited question.

  • Yes you will have to rewrite the whole file anyway, but with 2620 lines it is a relatively small file.

  • @lazyFox all right, because this file is bigger than I have, the rest are less than 2620 lines.

  • 1

    Let me see if I understand, you want to read the file and remove all the repeated digits within one >Produto [n]?

  • @Leandroangelo No, update only one line, because I already have value PRODUTO [2] and 2. stored in memory. Because sometimes comes a wrong word, then I want to correct.

  • @Matheusmiranda How do you want to find the line? By content, content, regex or other option?

  • @LINQ for content is enough.

  • @Matheusmiranda Why do you read the file that way? I guess I don’t quite understand what you’re doing... You have the file ready and want to make this fix, right?

  • @LINQ here is the answer because I read the file: https://answall.com/a/245852/54019 and Yes I want to do this correction just one line.

Show 10 more comments
No answers

Browser other questions tagged

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