Clear blanks of an array

3

I'm not finding the answer to this question, simply what I want to know is to remove empty positions from an array, for example:

var(0) = "Remover"
var(1) = ""
var(2) = "espaços"
var(3) = ""
var(4) = "em"
var(5) = ""
var(6) = "branco"

I want to remove the empty positions, which in this case would be var (1), var (3) and var (5),

Would anyone know how to do this?

I was thinking of passing only non-empty fields to another array, but how could I do that?

I tried to do the following command:

 Dim teste() As String
 Dim i1 As Integer = 1
 Dim i2 As Integer = 0
 Dim testesemespaco(500) As String

 teste = html.Split()

 While i1 < teste.Length

   If teste(i1) <> "" Then

        testesemespaco(i2) = teste(i1)

        i2 = i2 + 1

   End If

   i1 = i1 + 1

 End While
    
asked by anonymous 31.05.2017 / 17:56

1 answer

1

You can do this easily with LINQ, see the example.

It will also be possible to do with for by going through all the elements and creating another array or a list. In this case, it would be much better to create a list because they have dynamic allocation, so it will not be necessary to make two loops to leave only the valid elements in the collection.

Dim array = New String() {"Remover", "", "espaços", "", "em", "", "branco"}
Dim novoArray = array.Where(Function(palavra) Not String.IsNullOrEmpty(palavra)).ToArray()

For Each p As String In novoArray
    Console.WriteLine(p)
Next

The expression in Where reads as follows:

  

"For each palavra , being palavra an element of array return only one that is not null or empty".

See working in .NET Fiddle.

    
31.05.2017 / 18:22