Remove line from an array

1

How do I remove the line without getting whitespace?

N7 G90
N8 G26 RA 10.840
N9 G54 G92 X115.00 Y25.00

N11 C5
N12 D14
N13 G42 G01 X76.3276 Y-19.86 

Having to look like this:

N7 G90
N8 G26 RA 10.840
N9 G54 G92 X115.00 Y25.00
N11 C5
N12 D14
N13 G42 G01 X76.3276 Y-19.86 

My code for now looks like this:

//Remove Line Condition
if (_OS.Contains("M31"))
{
    _OS = _OS.Remove(0);
}
    
asked by anonymous 17.10.2016 / 14:18

3 answers

1

Alternatively, in addition to the option posted by Virgilio , if you are dealing with a heavy read line by file line, in this process you can skip lines that do not satisfy a certain condition, for example, use String.IsNullOrEmpty to check if the line is empty:

using System.Collections.Generic;
using System.IO;
//....

public static void Main(string[] args)
{
    List<string> linhas = new List<string>();

    using (StreamReader sr = new StreamReader("foo.txt"))
    {
        string linha;
        while ((linha = sr.ReadLine()) != null)
        {
            if (string.IsNullOrEmpty(linha)) // Aqui você pode incluir mais condições com ||
            {
                continue;
            }
            linhas.Add(linha);
        }
    }

    // Use "linhas" aqui...
}

A second alternative, if you're using lists , is the method List<T>.RemoveAll :

List<string> linhas = new List<string>();
// Inserindo valores na lista...

linhas.RemoveAll(linha => string.IsNullOrEmpty(linha));
    
17.10.2016 / 15:02
1

You could use System.IO.File.ReadAllLines to read all lines of the file and exclude those that are blank with Linq (% with%):

string[] arquivo = System.IO.File.ReadAllLines("arquivo.txt")
                .Where(c => !string.IsNullOrEmpty(c)).ToArray();

System.IO.File.WriteAllLines("arquivo.txt", arquivo);

If you want to create the file, do so with [Where](c => !string.IsNullOrEmpty(c)) ", where in this example, the file was rewritten. You can easily choose another name and stick with the original and change.

    
17.10.2016 / 14:27
1

You can use a lambda expression to do this

strArray = strArray.Where( x=>!x.Contains("M31")).ToArray();

It will get your array , see which of the items does not contain string "M31" and mount a new array with the output, removing any empty key.

    
17.10.2016 / 14:44