Services Partners Company
Knowledge Base


Article

How to determine if a character in a string is an integer

« Go Back

Information

 
EnvironmentProduct: Progress
Version: All supported versions
Product: OpenEdge
Version: All supported versions
OS: All supported platforms
Other: N/A
Question/Problem Description
How to determine if a character in a string is an integer
How to find out which individual characters in a string are numbers
Using the ASC function to determine if a character in a string is INTEGER
Steps to Reproduce
Clarifying Information
Error Message
Defect/Enhancement Number
Cause
Resolution

The best way to determine if a character is an integer is to check its underlying ASCII value.  To do this in the 4GL/ABL you can use the ASC function to determine if its ASCII value is between 48 (0) and 57 (9).

The following example demonstrates using a function to extract the digits from a string of a variety of characters.

DEFINE VARIABLE cString AS CHARACTER NO-UNDO.

FUNCTION extractDigits RETURNS CHARACTER ( INPUT pcString AS CHARACTER ):  
    /* Function name: extractDigits
       Parameters: pcString AS CHARACTER
       Desc: Takes an input string (pcString) and strips out and returns all numeric characters */

    DEFINE VARIABLE iChar AS INTEGER NO-UNDO.
    DEFINE VARIABLE iAsc AS INTEGER NO-UNDO.

    DEFINE VARIABLE cTemp AS CHARACTER NO-UNDO.
    DEFINE VARIABLE cChar AS CHARACTER NO-UNDO.

    DO iChar = 1 TO LENGTH(pcString):
        ASSIGN cChar = SUBSTRING(pcString,iChar,1)
                        iAsc = ASC(cChar).

        IF iAsc GT 47 AND
             iAsc LT 58 THEN
           cTemp = cTemp + cChar.
    END.

    IF (cTemp GT "") EQ TRUE THEN
        RETURN cTemp.
    ELSE
        RETURN ?. /* If no integers in the string return the unknown value. */
END FUNCTION.

cString = "sr6b h67jh6heww43t3v-4=790)%$^7h".
MESSAGE extractDigits(cString)
           VIEW-AS ALERT-BOX INFO BUTTONS OK.

 

Workaround
Notes
Attachment 
Disclaimer

The origins of the information on this site may be internal or external to Progress Software Corporation (“Progress”). Progress Software Corporation makes all reasonable efforts to verify this information. However, the information provided is for your information only. Progress Software Corporation makes no explicit or implied claims to the validity of this information.

Any sample code provided on this site is not supported under any Progress support program or service. The sample code is provided on an "AS IS" basis. Progress makes no warranties, express or implied, and disclaims all implied warranties including, without limitation, the implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample code is borne by the user. In no event shall Progress, its employees, or anyone else involved in the creation, production, or delivery of the code be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample code, even if Progress has been advised of the possibility of such damages.



Feedback
 
Was this article helpful?

   

Your feedback is appreciated.

Please tell us how we can make this article more useful.



Characters Remaining: 255