不断测试字符串中是否含有两个连续的空格,如果含有则将这两个空格替换成为一个空格,直到不含有两个连续空格为止Dim test As String test = "a b c" Do While InStr(1, test, " ") > 0 test = Replace(test, " ", " ") Loop Debug.Print test
Dim s As String s = Space(6) Do s = Replace(s, Space(2), Space(1)) Loop Until Len(s) = 1 MsgBox Len(s)
dim str as string dim strTemp as string dim i as integer '模拟c的做法 str="a b c" strTemp=left(str,1) i=2 while i<=len(str) if mid(str,i,1)="" then '空格 if(right(strTemp,1)<>"") then 'strtemp的最后一个不是空格 strTemp=strTemp & mid(str,i,1) end if else strTemp=strTemp & mid(str,i,1) end if i=i+1 wend
Private Sub Command1_Click() Dim I As Integer, S As String S = "1 2 6 7 9" For I = Len(S) To 2 Step -1 S = Replace(S, Space(I), Space(1)) Next MsgBox S End Sub
test = "a b c"
Do While InStr(1, test, " ") > 0
test = Replace(test, " ", " ")
Loop
Debug.Print test
s = Space(6)
Do
s = Replace(s, Space(2), Space(1))
Loop Until Len(s) = 1
MsgBox Len(s)
dim strTemp as string
dim i as integer
'模拟c的做法
str="a b c"
strTemp=left(str,1)
i=2
while i<=len(str)
if mid(str,i,1)="" then '空格
if(right(strTemp,1)<>"") then 'strtemp的最后一个不是空格
strTemp=strTemp & mid(str,i,1)
end if
else
strTemp=strTemp & mid(str,i,1)
end if
i=i+1
wend
Dim I As Integer, S As String
S = "1 2 6 7 9"
For I = Len(S) To 2 Step -1
S = Replace(S, Space(I), Space(1))
Next
MsgBox S
End Sub
其实一个语句就够了,例如
Replace("asdf sdf sdf",space(1),"")