VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



This simple code removes leading, trailing and inner extra spaces from a string, e.g. the string

by Kostas (1 Submission)
Category: String Manipulation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 4th April 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)

This simple code removes leading, trailing and inner extra spaces from a string, e.g. the string " hello to you " will be converted to "hello

Rate This simple code removes leading, trailing and inner extra spaces from a string, e.g. the string



    Dim myStr As String
    myStr = str
    
    ' Remove leading spaces
    While (Left(myStr, 1) = " ")
        myStr = Mid(myStr, 2, Len(myStr) - 1)
    Wend
    
    ' Remove trailing spaces
    While (Right(myStr, 1) = " ")
        myStr = Left(myStr, Len(myStr) - 1)
    Wend
    
    ' Every two adjacent words are separated by one space only
    Dim Position As Integer
    Dim EndOfString As Boolean
    Position = 1
    EndOfString = False
    While EndOfString = False
        While Mid(myStr, Position, 1) <> " " And Position < Len(myStr)
            Position = Position + 1
        Wend
        If Mid(myStr, Position, 1) = " " And Mid(myStr, Position + 1, 1) = " " Then
            myStr = Left(myStr, Position - 1) & Mid(myStr, Position + 1, Len(myStr) - Position)
            Position = 1
        End If
        If Position = Len(myStr) Then
            EndOfString = True
        Else
            Position = Position + 1
        End If
    Wend
    
    RemoveExtraSpaces = myStr
End Function


Download this snippet    Add to My Saved Code

This simple code removes leading, trailing and inner extra spaces from a string, e.g. the string Comments

No comments have been posted about This simple code removes leading, trailing and inner extra spaces from a string, e.g. the string . Why not be the first to post a comment about This simple code removes leading, trailing and inner extra spaces from a string, e.g. the string .

Post your comment

Subject:
Message:
0/1000 characters