UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> FixText Digits    


This function uses the Replace() function to remove characters that are not numeric digits from the input string.

Related Functions: Strip_Unwanted_Characters FixText_Letters

' FixText Digits
' http://www.utteraccess.com/wiki/FixText_Digits
' Code courtesy of UtterAccess Wiki
' Licensed under Creative Commons License
' http://creativecommons.org/licenses/by-sa/3.0/
' You are free to use this code in any application,
' provided this notice is left unchanged.
' rev  date                          brief descripton
' 1.0  2016-07-04                    Replacing unwanted characters
Public Function FixText_Digits(strInput As String) As String
'Purpose    : remove all non-numeric characters from input string
'Author     : UA Wiki
'Description: loops through the input string replacing non-digits with
'             placeholder character then replaces the placeholder
'             with empty string character. The placeholder is used so
'             that the result string length does not change until
'             all characters have been inspected
'Useage     : strOutput = FixText_Digits(strInput)
'Date       : 04Jul16

Const cstrEMPTY = ""            'empty string
Const cstrPLACEHOLDER = ":"     'temporary placeholder
'                                - colon used here - bar character is probably better
   Dim strResult As String     'string to hold result
   Dim strChar As String       'string to hold current character
   Dim intCounter As Integer   'counter used to control loop

   On Error GoTo HandleError
   'initialize result string
   strResult = strInput
   'loop through the result string
   For intCounter = 1 To Len(strResult)
       strChar = (Mid(strResult, intCounter, 1))
       'replace non-digit with placeholder
       If Asc(strChar) < Asc("0") _
           Or Asc(strChar) > Asc("9") Then
           strResult = Replace(strResult, strChar, cstrPLACEHOLDER)
       End If
   Next intCounter
   'replace the placeholder with empty string
   strResult = Replace(strResult, cstrPLACEHOLDER, cstrEMPTY)
   'return the fixed string
   FixText_Digits = strResult
   Exit Function
   MsgBox Err.Description
   Resume HandleExit

End Function

Creative Commons License
FixText Digits by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Edit Discussion
Custom Search

Thank you for your support!
This page has been accessed 1,880 times.  This page was last modified 15:59, 4 July 2016 by Glenn Lloyd.   Disclaimers