Add number in each row in c#

Asked

Viewed 116 times

3

How to remove the numbering of this line and then add again in another format?

Input:
N0006 G90
N0007 G90
N0008 G92 X21.7301 Y88.9657
N0009 S555
N0010 D14
N0011 G42 G01 X22.0659 Y89.3015 
N0012 X22.4194 Y89.655 
N0013 (PATHSTART0)

Output(thus remaining):

N1 G90
N2 G90
N3 G92 X21.7301 Y88.9657
N4 S555
N5 D14
N6 G42 G01 X22.0659 Y89.3015 
N7 X22.4194 Y89.655 
N8 (PATHSTART0)

Would I have to go through the right text file? But how can I identify the numbering of the beginning? Sometimes these codes reach up to 10thousand lines?

  • So dude, in this https://www.machsupport.com/forum/index.php?action=dlattach;topic=27728.0;attach=38344 he does exactly what I want.. it removes the lines with N and then adds again, the problem is that I have no idea how to do this understands?

  • 1

    Look, I don’t understand it very well, but if it’s in this pattern, you can make a scheme to remove everything before the space, and then include everything again by incrementing 1:1 in front of the N for each time you pass the loop

  • Diegoaugusto.. Yes, that’s right, and how can I identify a space in a string and do that ?

  • @stderr almost that... sometimes the lines can reach up to 50k understand? so I’m trying to do the following here.. all values before space ' ', I wanted to delete... you have any idea?

  • thus: N0008 G92 X21.7301 Y88.9657 Thus: G92 X21.7301 Y88.9657..... removing the N0008 only in front of the string

  • Then.. as soon as I removed the before.. I would go through the array again to add that way understand?

  • It worked out here. The way I wanted it... Thank you so much for your time!

Show 2 more comments

2 answers

4


Here’s what I’d do:

If you are sure that the pattern always includes "N" at the beginning:

string aux = linha.split(' ')[0];
string numeracao = aux.substring(1);

In case you wanted a whole:

string aux = linha.split(' ')[0];
int numeracao = Int32.Parse(aux.substring(1));

If you have a block of text and want to split the lines:

string[] separador = new string[] { "\r\n" };
string[] linhas = texto.Split(separador, StringSplitOptions.None);
foreach(string linha in linhas)
{
  string aux = linha.split(' ')[0];
  int numeracao = Int32.Parse(aux.substring(1));
}
  • And how do I do this. Delete everything you have before the first space ?

  • I would use the substring method: string aux = line.split(' ')[0]; string semnumbering = line.substring(aux.lenght + 1);

  • Also worked out here hehe... Thanks!!!

3

To open the file for reading you can use the method File.OpenText.

The File.OpenText returns a StreamReader, whereas it is a file with many lines, use the method StreamReader.ReadLine to read line by line on loop:

string linha;

using(var arquivo = File.OpenText("arquivo1.txt")) {
    while ((linha = arquivo.ReadLine()) != null) {
        // Use "linha" aqui ...
    }
}

To get the sequence before the first space, use the method String.IndexOf:

string linha = "foo bar baz";
int indice = linha.IndexOf(" "); // 3. O número de caracteres antes do primeiro espaço

With the character index you want to remove, use the method String.Substring to return the sequence from that index:

string linha = "foo bar baz";
int indice = linha.IndexOf(" ");

string outraLinha = "bar" + linha.Substring(indice);

Console.WriteLine(linha);      // foo bar baz
Console.WriteLine(outraLinha); // bar bar baz

In your specific case, you can apply like this:

using System.IO;
// ...

string linha;
int contador = 0;

using(var arquivo = File.OpenText("foobar.txt")) { // Abre o arquivo para leitura
    while ((linha = arquivo.ReadLine()) != null) { // Lê linha por linha
        int espaco = linha.IndexOf(" "); // Quantidade de caracteres antes do espaço
        contador++; // Incrementa o número da linha atual
        // Retorna os caracteres a partir do primeiro espaço
        string linhaIndexada = "N" + contador + linha.Substring(espaco); 

        // Use "linhaIndexada" aqui ...
    }
}

See DEMO

Browser other questions tagged

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