Trim "inbetween" whitespace

  • Thread starter Thread starter Raterus
  • Start date Start date
R

Raterus

Hello,

I'd like to take a string like this "one two three four five"
and make it like this "one two three four five"

essentially replacing >1 spaces with 1. Can I do this somehow with the built-in functions?, I haven't found a way yet. I've tried using replace(" ", " "), but this has to be ran an unknown amount of times to trim down the string.

Thanks,
--Michael
 
Gary,
Do While (MyString.IndexOf(" ") >= 0)
MyString = MyString.Replace(" ", " ")
Loop

That code would potentially create a lots of temporary strings, which
can hurt performance.


Michael,

I'd use Regexp or code my own function for it, something like this

Function InTrim(s As String, Optional remove As Char = " "c) As String
Dim buf(s.Length - 1) As Char
Dim n As Integer
Dim skip As Boolean

For Each c As Char In s
If Not skip OrElse c <> remove Then
skip = c = remove
buf(n) = c
n += 1
End If
Next
Return New String(buf, 0, n)
End Function

....

Console.WriteLine(InTrim("one two three four five"))



Mattias
 
Gary Miltonwrote:
Hi Michael,
Try this...

\\\
Dim MyString As String = "one two three four five"

Do While (MyString.IndexOf(" ") >= 0)
MyString = MyString.Replace(" ", " ")
Loop
///

HTH,
Gary

I believe the original question asked for any approach other than the
one you gave. I don't believe there is any way to solve this problem
more efficiently than an unknown number of replaces. (you could use
regular expressions, but my experience indicates that they would not
be nearly as efficient as the indicated loop).

I'd be curious to see if anyone has an alternative solution.
 
Jimi said:
I believe the original question asked for any approach other than the
one you gave. I don't believe there is any way to solve this problem
more efficiently than an unknown number of replaces. (you could use
regular expressions, but my experience indicates that they would not
be nearly as efficient as the indicated loop).

I'd be curious to see if anyone has an alternative solution.

If you are REALLY interested, and have a mind for math, check out
this post (and the surrounding thread it is in):

http://groups.google.com/[email protected]#link28

(Be sure the whole link is used)

That very topic was discussed not long ago. To set up what he was going
after, you might want to scan through some of the posts from Bob O'Bob,
earlier in that thread. I had mine own results, but the guy above came in
with mathematics and pretty well ended the disucssion (with a calculated
answer).

<g>
LFS
 
Jimi said:
I believe the original question asked for any approach other than the
one you gave. I don't believe there is any way to solve this problem
more efficiently than an unknown number of replaces. (you could use
regular expressions, but my experience indicates that they would not
be nearly as efficient as the indicated loop).

I'd be curious to see if anyone has an alternative solution.

Dim blnSpace as boolean
Dim i, j, intLen as integer
Dim MyString As String = "one two three four five"
Dim NewString as String

intLen = len(MyString)

for i = 0 to (intLen - 1)
if mid(MyString,i,1) = " " then
if blnSpace then
else
blnSpace = true
mid(NewString,j,1) = mid(MyString,i,1)
j += 1
else
blnSpace = false
mid(NewString,j,1) = mid(MyString,i,1)
j += 1
end if
next i

MyString = NewString

This would loop through the string only one time, byte by byte,
eliminating any subsequent spaces found after a space is found (then
resetting the boolean when any other character is found). I suspect you
could += each character when adding it to NewString, but I'm not positive
that using that method won't lose spaces as you add them to the end of the
string.

Regis
 
Back
Top