用正则表达式做压缩~ 'Refer to:Microsoft VBScript Regular Expressions 5.5. Dim regexpObj As New RegExp regexpObj.Pattern = "[ ]{1,}" //这就能找到最少是一个空格的所有空格字串 regexpObj.Global = True Dim matches,match,ret Set matches = regexpObj.Execute("adfjaslkdfjp你好sdgbvsdgsdfgsd") 'Also see to SubMatches Collection. For Each match in matches ret = ret & vbCrLf & "Match found at " & match.FirstIndex & " Value is " & match.Value & "." Next MsgBox ret
方法一,循环判断 Private Sub Command1_Click() Dim a As String Dim i As Integer Dim strText As String a = "adewr d cposdo dforeiore;errekfrelr r" For i = 1 To Len(a) If Asc(Mid(a, i, 1)) <> 32 Then strText = strText & Mid(a, i, 1) Next MsgBox strText End Sub
或者切割成为数组再重新联合,注意联合不要用join去做Private Sub Command1_Click() Dim a As String Dim B() As String Dim i As Integer Dim strText As String
a = "adewr d cposdo dforeiore;errekfrelr r"
B() = Split(a, " ")
For i = 0 To UBound(B) strText = strText & B(i) Next Debug.Print strText End Sub
str = "a b c d e f g h"Text1 = replace(str, " ", "")
楼主是说的字符串前面和后面的空格吧 用trim ltrim rtrim 吧
Private Sub Form_Load() 'Refer to:Microsoft VBScript Regular Expressions 5.5. Dim regexpObj As New RegExp regexpObj.Pattern = "[ ]{1,}" '这就能找到最少是一个空格的所有空格字串 regexpObj.Global = True MsgBox regexpObj.Replace("adfjas lkdfjp sdgbv sdgsdfgsd", "") End Sub
'Refer to:Microsoft VBScript Regular Expressions 5.5.
Dim regexpObj As New RegExp
regexpObj.Pattern = "[ ]{1,}" //这就能找到最少是一个空格的所有空格字串
regexpObj.Global = True
Dim matches,match,ret
Set matches = regexpObj.Execute("adfjaslkdfjp你好sdgbvsdgsdfgsd")
'Also see to SubMatches Collection.
For Each match in matches
ret = ret & vbCrLf & "Match found at " & match.FirstIndex & " Value is " & match.Value & "."
Next
MsgBox ret
Private Sub Command1_Click()
Dim a As String
Dim i As Integer
Dim strText As String a = "adewr d cposdo dforeiore;errekfrelr r" For i = 1 To Len(a)
If Asc(Mid(a, i, 1)) <> 32 Then strText = strText & Mid(a, i, 1) Next MsgBox strText
End Sub
Dim a As String
Dim B() As String
Dim i As Integer
Dim strText As String
a = "adewr d cposdo dforeiore;errekfrelr r"
B() = Split(a, " ")
For i = 0 To UBound(B)
strText = strText & B(i)
Next
Debug.Print strText
End Sub
用trim ltrim rtrim 吧
'Refer to:Microsoft VBScript Regular Expressions 5.5.
Dim regexpObj As New RegExp
regexpObj.Pattern = "[ ]{1,}" '这就能找到最少是一个空格的所有空格字串
regexpObj.Global = True
MsgBox regexpObj.Replace("adfjas lkdfjp sdgbv sdgsdfgsd", "")
End Sub