Article ID: 148396
Article Last Modified on 1/19/2007
Option Explicit
Function StripExtraChars (PassedStr, RemoveExtraChar$)
On Local Error GoTo StripExtraChars_Err
Dim i As Integer, GotChar As Integer
Dim HoldStr As String, HoldChar As String
' Exit if passed value is Null.
If IsNull(PassedStr) Then Exit Function
' Trim extra characters from passed string.
PassedStr = Trim$(PassedStr)
' Cycle through string and remove extra
' string characters specified in the
' RemoveExtraChar value.
For i = 1 To Len(PassedStr)
HoldChar = Mid$(PassedStr, i, 1)
If HoldChar = RemoveExtraChar Then
If GotChar = False Then
GotChar = True
HoldStr = HoldStr & HoldChar
End If
Else
GotChar = False
End If
If Not GotChar Then
HoldStr = HoldStr & HoldChar
End If
Next i
StripExtraChars = HoldStr
StripExtraChars_End:
Exit Function
StripExtraChars_Err:
MsgBox Error$
Resume StripExtraChars_End
End Function
?StripExtraChars(" My Test String ", Chr$(32))
Keywords: kbhowto kbprogramming kbusage KB148396