Results 1 to 13 of 13

Thread: Removing Double Blanks in String

  1. #1

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Question 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

  2. #2
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Arrow Re: Removing Double Blanks in String

    Use Replace Function

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Removing Double Blanks in String

    VB Code:
    1. Do While InStr(YourString, "  ")
    2.     YourString = Replace(YourString, "  ", " ")
    3. Loop

  4. #4

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Angry Re: Removing Double Blanks in String

    only Replace did not work
    VB Code:
    1. replace("tes ssss   trx    ssss     xxxx            ssssss","  "," ")
    2.  
    3. tes ssss  trx  ssss   xxxx      ssssss

    I do not want to use a LOOP

  5. #5
    Hyperactive Member
    Join Date
    Apr 2004
    Posts
    342

    Re: Removing Double Blanks in String

    Loop is the only way....

  6. #6

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Thumbs up Re: Removing Double Blanks in String



    I find (in Brazil call : GAMBIARRA or RTA (Resource Techinical Alternative))


    VB Code:
    1. replace(replace(replace("tes ssss   trx   ssss","  "," _"),"_ ","_"),"_","")

  7. #7
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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?

  8. #8

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Talking Re: Removing Double Blanks in String

    BOSS


    IF string already has underscores I am lost

  9. #9
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    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...

  10. #10
    Hyperactive Member
    Join Date
    Apr 2004
    Posts
    342

    Re: Removing Double Blanks in String

    And from What I can see, really the only solution....

  11. #11
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  12. #12
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Debug.Print RemoveChar("tes ssss   trx    ssss     xxxx            ssssss")
    5. End Sub
    6.  
    7. Private Function RemoveChar(Str As String, Optional Char As String = " ") As String
    8.     Dim Buff() As Byte, K As Long, I As Long
    9.     Dim CharByte As Byte
    10.    
    11.     CharByte = Asc(Left(Char, 1))
    12.     Buff = StrConv(Str, vbFromUnicode)
    13.    
    14.     Do
    15.         If Buff(K) = CharByte Then
    16.             Do While Buff(K) = CharByte
    17.                 K = K + 1
    18.                 If K >= UBound(Buff) Then Exit Do
    19.             Loop
    20.             K = K - 1
    21.             If K >= UBound(Buff) Then Exit Do
    22.         End If
    23.        
    24.         If K <> I Then Buff(I) = Buff(K)
    25.         I = I + 1
    26.         K = K + 1
    27.     Loop Until K > UBound(Buff) Or I > UBound(Buff)
    28.  
    29.     ReDim Preserve Buff(I - 1)
    30.    
    31.     RemoveChar = StrConv(Buff, vbUnicode)
    32. End Function

  13. #13
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Removing Double Blanks in String

    VB Code:
    1. CharByte = Asc(Left(Char, 1))
    can be simply done as
    VB Code:
    1. CharByte  = Asc(Char)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width