|
-
Nov 22nd, 2006, 07:33 AM
#1
Thread Starter
Fanatic Member
Removing Double Blanks in String
Hi
How can I to remove double Blanks in String
Example:
Code:
myvar= "tes ssss trx ssss xxxx ssssss"
I want
myvar= "tes ssss trx ssss xxxx ssssss"
thanks
-
Nov 22nd, 2006, 07:34 AM
#2
Re: Removing Double Blanks in String
Use Replace Function
-
Nov 22nd, 2006, 07:40 AM
#3
Re: Removing Double Blanks in String
VB Code:
Do While InStr(YourString, " ")
YourString = Replace(YourString, " ", " ")
Loop
-
Nov 22nd, 2006, 07:40 AM
#4
Thread Starter
Fanatic Member
Re: Removing Double Blanks in String
only Replace did not work
VB Code:
replace("tes ssss trx ssss xxxx ssssss"," "," ")
tes ssss trx ssss xxxx ssssss
I do not want to use a LOOP
-
Nov 22nd, 2006, 07:47 AM
#5
Hyperactive Member
Re: Removing Double Blanks in String
-
Nov 22nd, 2006, 07:51 AM
#6
Thread Starter
Fanatic Member
Re: Removing Double Blanks in String
-
Nov 22nd, 2006, 08:04 AM
#7
Re: Removing Double Blanks in String
and what happens if the string already has underscores in it...?
why don't you want to use a loop?
-
Nov 22nd, 2006, 08:21 AM
#8
Thread Starter
Fanatic Member
-
Nov 22nd, 2006, 08:25 AM
#9
Re: Removing Double Blanks in String
I really can't see why you don't want to use the loop since it's prodiving a perfect solution...
-
Nov 22nd, 2006, 09:03 AM
#10
Hyperactive Member
Re: Removing Double Blanks in String
And from What I can see, really the only solution....
-
Nov 22nd, 2006, 09:15 AM
#11
Re: Removing Double Blanks in String
mutley, you don't know how many spaces there are... three calls to replace may not be enough. That's why your testing that in the Do loop with this line
Code:
Do While InStr(YourString, " ")
Which translates as, "as long as there are two adjacent spaces in string, run the iteration." And the iteration consists of calling replace.
-
Nov 22nd, 2006, 01:14 PM
#12
Re: Removing Double Blanks in String
I just wrote this, it is faster than using Replace in a loop, and really fast when it comes to large strings:
VB Code:
Option Explicit
Private Sub Form_Load()
Debug.Print RemoveChar("tes ssss trx ssss xxxx ssssss")
End Sub
Private Function RemoveChar(Str As String, Optional Char As String = " ") As String
Dim Buff() As Byte, K As Long, I As Long
Dim CharByte As Byte
CharByte = Asc(Left(Char, 1))
Buff = StrConv(Str, vbFromUnicode)
Do
If Buff(K) = CharByte Then
Do While Buff(K) = CharByte
K = K + 1
If K >= UBound(Buff) Then Exit Do
Loop
K = K - 1
If K >= UBound(Buff) Then Exit Do
End If
If K <> I Then Buff(I) = Buff(K)
I = I + 1
K = K + 1
Loop Until K > UBound(Buff) Or I > UBound(Buff)
ReDim Preserve Buff(I - 1)
RemoveChar = StrConv(Buff, vbUnicode)
End Function
-
Nov 22nd, 2006, 01:20 PM
#13
Re: Removing Double Blanks in String
VB Code:
CharByte = Asc(Left(Char, 1))
can be simply done as
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|