"Jimi" <ji************@yahoo-dot-ca.no-spam.invalid> wrote in message
news:40**********@127.0.0.1...
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.
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