Replace unwanted characters in a string

Started by nandagopal, Nov 06, 2008, 04:10 PM

Previous topic - Next topic

nandagopal

Loop through the characters. For each, see if it is in a string containing the unwanted characters. If a character is unwanted, replace it with the replacement character.


' Replaces the characters set in strUnwanted as spaces
Public Function RemoveCharacters(ByRef strText As String, _
    ByRef strUnwanted As String) As String
Dim currLoc As Integer
Dim StringLength As Integer
Dim tmpChar As String

    StringLength = Len(strText)
    For currLoc = 1 To StringLength
        tmpChar = Mid(strText, currLoc, 1)
        If InStr(strUnwanted, tmpChar) Then
            ' Replace with a space
            Mid(strText, currLoc, 1) = " "
        End If
    Next

    RemoveCharacters = strText
End Function