Can I remove all non numeric characters from a string?

shiv_379
06-11-2004, 04:46 AM
Greetings all!
Quick question, is there a way that I can remove all the non-numeric characters from a string? Preferably something built into VBA already, but if anyone has any ideas for a descent function to do it I'd be interested in that also.

Thanks!
-Shiv

tinyjack
06-11-2004, 07:08 AM
If there is no pattern to the string the easiest option would be to loop through the string a test each character. However, if there is a pattern you might be able to use a different approach.

TJ

Kluz
06-11-2004, 11:51 AM
We have a thread on this subject here (http://www.visualbasicforum.com/showthread.php?t=145470).

Ivan F Moala
06-12-2004, 09:11 PM
You could Use the Built in RegExp of Vbscript

something like this;


Option Explicit

Public Function fnGetCharacters(strA As String, blnNum As Boolean) As String
Dim objRegExp As Object
Dim objcolRegMatch As Object
Dim objRegMatch As Object

'// Create the RegExp Object using Late binding
Set objRegExp = CreateObject("Vbscript.RegExp")

With objRegExp
'// Set as Global to get ALL instances
.Global = True
If blnNum Then
'// \d+ Matches a digit character of any length.
.Pattern = "\d+"
Else
'// \D Matches a nondigit character of any length.
.Pattern = "\D+"
End If
End With

Set objcolRegMatch = objRegExp.Execute(strA)
For Each objRegMatch In objcolRegMatch
fnGetCharacters = fnGetCharacters & objRegMatch
Next

'// Clean up
Set objRegExp = Nothing
Set objcolRegMatch = Nothing

End Function

Sub Tester()
MsgBox fnGetCharacters("1421,<>2?\|*&%$#@!4as53", True), vbInformation, "1421,<>2?\|*&%$#@!4as53" & " True"
MsgBox fnGetCharacters("1TasaSQW/?s5421.89", True), vbInformation, "1TasaSQW/?s5421.89" & " True"
MsgBox fnGetCharacters("1zX2453|asas", True), vbInformation, "1zX2453|asas" & " True"
MsgBox fnGetCharacters("1421,<>2?\|*&%$#@!4as53", False), vbInformation, "1421,<>2?\|*&%$#@!4as53" & " False"
MsgBox fnGetCharacters("1TasaSQW/?s5421.89", False), vbInformation, "1TasaSQW/?s5421.89" & " False"
MsgBox fnGetCharacters("1zX2453|asas", False), vbInformation, "1zX2453|asas" & " False"
End Sub

Volte
06-12-2004, 09:45 PM
A slightly cleaner way to do that is like this:With objRegExp
'// Set as Global to get ALL instances
.Global = True
If blnNum Then
'// \d+ Matches a digit character of any length.
.Pattern = "[^\d]+"
Else
'// \D Matches a nondigit character of any length.
.Pattern = "[^\D]+"
End If
End With

fnGetCharacters = objRegExp.Replace(strA, "")It simply runs a regex replace and kills all (non)numeric characters. No need for a loop.

Ivan F Moala
06-12-2004, 11:14 PM
A slightly cleaner way to do that is like this:With objRegExp
'// Set as Global to get ALL instances
.Global = True
If blnNum Then
'// \d+ Matches a digit character of any length.
.Pattern = "[^\d]+"
Else
'// \D Matches a nondigit character of any length.
.Pattern = "[^\D]+"
End If
End With

fnGetCharacters = objRegExp.Replace(strA, "")It simply runs a regex replace and kills all (non)numeric characters. No need for a loop.

Yes, good catch :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum