如何将字符串中的连续空白替换为一个空格
在其他的语言中用正则表达式可以很容易实现,不知道VB中该如何做呢?

解决方案 »

  1.   

    不断测试字符串中是否含有两个连续的空格,如果含有则将这两个空格替换成为一个空格,直到不含有两个连续空格为止Dim test As String
    test = "a               b         c"
    Do While InStr(1, test, "  ") > 0
        test = Replace(test, "  ", " ")
    Loop
    Debug.Print test
      

  2.   

    Dim s As String
    s = Space(6)
    Do
      s = Replace(s, Space(2), Space(1))
    Loop Until Len(s) = 1
    MsgBox Len(s)
      

  3.   

    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
      

  4.   

    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
      

  5.   


    其实一个语句就够了,例如
    Replace("asdf sdf   sdf",space(1),"")