Results 1 to 6 of 6

Thread: Remove double spaces between words(Resolved)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Resolved Remove double spaces between words(Resolved)

    Hi,

    I have a textBox for the user to key in their name. Please help me how do remove or avoid the double spaces between words.

    Thanks !
    Vivian
    Last edited by vivian2u; Apr 2nd, 2005 at 03:52 AM.

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Remove double spaces between words

    First, add this code to your project...it will remove all instances of 2 or more spaces, so there will only be 1 space.

    VB Code:
    1. Function SingleSpaces(ByVal TextString As String) As String
    2. Dim lonLen As Long, lonLoop As Long
    3. Dim strCur As String, strNext As String
    4. Dim strTemp As String, strRet As String
    5.  
    6. strTemp = TextString
    7. lonLen = Len(strTemp)
    8.  
    9. If lonLen = 0 Then Exit Function
    10.  
    11. For lonLoop = 1 To lonLen
    12.     strCur = Mid$(strTemp, lonLoop, 1)
    13.    
    14.     If strCur = " " Then
    15.         strNext = Mid$(strTemp, lonLoop + 1, 1)
    16.        
    17.         If Not strNext = " " Then
    18.             strRet = strRet & strCur
    19.         End If
    20.        
    21.     Else
    22.         strRet = strRet & strCur
    23.     End If
    24.  
    25. Next lonLoop
    26.  
    27. SingleSpaces = strRet
    28. End Function

    Then, you can remove all spaces by using the code:

    VB Code:
    1. TextBox.Text = SingleSpaces(TextBox.Text)

    Where TextBox is the name of the textbox the user enters their name into.

  3. #3
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Remove double spaces between words

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command_Click()
    4.     If InStr(Text.Text, "  ") Then
    5.         MsgBox "instr"
    6.        Text.Text = Replace(Text.Text, "  ", " ")
    7.     End If
    8. End Sub

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Re: Remove double spaces between words

    Hi,

    Thanks for the help, but the problem is when the user press the space bar twice, the cursor will move back to the beginning of the words. How to solve the problem ? It has to avoid the user to press the spaces bar more than once..

    vivian

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Remove double spaces between words

    VB Code:
    1. Private Sub txtName_KeyPress(KeyAscii As Integer)
    2. If KeyAscii = 32 Then
    3.    
    4.     If Len(Text1.Text) > 0 Then
    5.        
    6.         If Right$(Text1.Text, 1) = " " Then
    7.             KeyAscii = 0
    8.         End If
    9.    
    10.     End If
    11.  
    12. End If
    13. End Sub

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Re: Remove double spaces between words

    Thank you. Thats exactly what I want.


    vivian

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