Trim String, remove spaces between chars

  • Thread starter Thread starter MsYang
  • Start date Start date
Status
Not open for further replies.
M

MsYang

Guest
I've attached a trimmed string example. The module will trim spaces in front of text, after the text and spaces between text that has 2 or more spaces.

It seems like whenever I need this code, I cant seem to find it anywhere, so thought I share it with everyone.
 

Attachments

Hi -

Like this?
Code:
Function OneSpace(pstr As String) As String

'*******************************************
'Purpose:   Removes excess spaces from a string
'Input:     ? onespace(" now    is  the  time for   all good men  ")
'Output:    "now is the time for all good men"
'*******************************************

Dim strHold As String
    strHold = Trim(pstr)
    Do While InStr(strHold, "  ") > 0
      strHold = Left(strHold, InStr(strHold, "  ") - 1) & Mid(strHold, InStr(strHold, "  ") + 1)
    Loop
    OneSpace = strHold
    
End Function

Bob
 
Last edited:
Status
Not open for further replies.

Users who are viewing this thread

Back
Top Bottom