Strip all non alpha characters from a string

  • Thread starter Thread starter Ken Snell
  • Start date Start date
K

Ken Snell

Function StripAlphaChars(strOriginalString As String) As String
Dim intLoop As Integer
Dim strTemp As String
strTemp = strOriginalString
For intLoop = Asc("A") To Asc("Z")
strTemp = Replace(strTemp, Chr(intLoop), "", 1, -1, vbTextCompare)
Next intLoop
StripAlphaChars = strTemp
End Function
 
Oops -- Sorry, I misread your post. This will strip numeric characters from
the string. If you want to cover other alpha characters, such as French or
German alpha characters, you can modify the For loop parameters as needed.

Full "width" of ASCII characters is 0 to 255 for Chr(x) function (x = 0 to
255).


Function StripNumberChars(strOriginalString As String) As String
Dim intLoop As Integer
Dim strTemp As String
On Error Resume Next
strTemp = strOriginalString
For intLoop = Asc("0") To Asc("9")
strTemp = Replace(strTemp, Chr(intLoop), "", 1, -1, vbTextCompare)
Next intLoop
StripNumberChars = strTemp
Err.Clear
End Function
 
All,

Is there such a function that can strip all non alpha ( not between a-z)
characters from a string? I have a function that I currently use that will
strip one character at a time from a string that I tried tweaking without
success. Any help would be much appreciated.

Thanks in advance,
Mark C.
(e-mail address removed)
 
There are always, of course, lots of ways of doing these things. Here's
another approach (adapted from Ken's):


Function StripAlphaChars(strOriginalString As String) As String
Dim intLoop As Integer
Dim strTemp As String
Dim strChar As String
strTemp = ""
For intLoop = 1 To Len(strOriginalString)
strChar = Mid$(strOriginalString, intLoop, 1)
Select Case Asc(strChar)
' Upper case chars [A-Z]
Case 65 To 90: strTemp = strTemp & strChar
' Lower case chars [a-z]
Case 97 To 122: strTemp = strTemp & strChar
Case Else
End Select
Next intLoop
StripAlphaChars = strTemp
End Function

HTH,
Randy
 
a-z)

Guess I misunderstood. I thought that meant that you might want to strip
characters besides digits, such as punctuation and control characters, as
well.
 
Back
Top