我打算按字母排序输入的字符串,其中可能有空格,而且其个数不定,我该如何剔除呢?谢谢大大。

解决方案 »

  1.   

    用正则表达式做压缩~ 
        '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
      

  2.   

    方法一,循环判断
    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
      

  3.   

    或者切割成为数组再重新联合,注意联合不要用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
      

  4.   

    str = "a b c      d e f    g    h"Text1 = replace(str, " ", "")
      

  5.   

    楼主是说的字符串前面和后面的空格吧
    用trim ltrim rtrim 吧
      

  6.   

    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