tags:

views:

69

answers:

2

How do I trim all whitespace in an array?

Dim str() As String = {"Tyrannosaurus", _
       "Amarga saurus", _
       " Mamenchisaurus", _
       "Brachios aurus", _
       "Deinonychus", _
       "Tyr annosaurus", _
       " Compsognathus"}
+1  A: 
Dim reg As New Regex("\s*")
For i = 0 To temp.Length - 1
    temp(i) = reg.Replace(temp(i), "")
Next i
Phaedrus
`\s+` would perhaps work slightly better.
strager
You could also use a foreach loop.
lc
A: 
str = str.Select(Function(s) s.Replace(" ", "")).ToArray()
Jason
Curiosity question, but what's the cost of making a new array vs editing the current one in place? Does the compiler optimize it to the same thing?
lc
Micro-optimization 99% of the time. Allocations in .NET are extremely fast.
Josh Einstein