Luca Ruggeri
Luca Ruggeri

Reputation: 121

Remove char on each elements of list

I have a list as below. The list contains n elements. I would like to clear each element of the list removing the "[" or "]" char

Dim bracList As New System.Collections.Generic.List(Of String)({"[1_1,2,3", "", "]"})

For i As Integer = 0 To bracList.Count -1
   bracList(i) = bracList(i).Replace("[","").Replace("]","")
Next i

The above code works for small list but if I have a big list it takes too long.

Upvotes: 0

Views: 146

Answers (1)

Daan
Daan

Reputation: 76

As already mentioned, using regular expressions will result in a significant performance improvement, especially for large lists. You could also use Parallel.Foreach. I don't know how much faster it will be in this case, but theoretically it should be faster:

Dim bracList As New System.Collections.Generic.List(Of String)({"[1_1,2,3", "", "]"})
Threading.Tasks.Parallel.ForEach(bracList, Sub(item)
                                                   item.Replace("[", "").Replace("]", "")
                                               End Sub)

Upvotes: 1

Related Questions