Babu Kumarasamy
Babu Kumarasamy

Reputation: 329

Trim all whitespace in an array

How do I trim all whitespace in an array?

Dim str() As String = {"Tyrannosaurus", _
       "Amarga saurus", _
       " Mamenchisaurus", _
       "Brachios aurus", _
       "Deinonychus", _
       "Tyr annosaurus", _
       " Compsognathus"}

Upvotes: 1

Views: 5641

Answers (3)

Kit Ramos
Kit Ramos

Reputation: 1859

I Like Jason's answer but that will remove all spaces everywhere. if you only want to remove the starting and trailing spaces and keep the ones in the middle you need to change it slightly.

str = str.Select(Function(s) s.Trim).ToArray()

this way "Brachios aurus" is not affected because the space is inside, but " Compsognathus" will have that space in the begging removed.

it does seem like you can put in any string function in there and it'll apply it to all the items in the array

Upvotes: 1

jason
jason

Reputation: 241641

str = str.Select(Function(s) s.Replace(" ", "")).ToArray()

Upvotes: 3

Phaedrus
Phaedrus

Reputation: 8421

Dim reg As New Regex("\s*")
For i = 0 To temp.Length - 1
    temp(i) = reg.Replace(temp(i), "")
Next i

Upvotes: 1

Related Questions