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
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 
Feedback
 
Was this article helpful?

   

Your feedback is appreciated.

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



Characters Remaining: 255

 



Copyright © 1993-2014. Progress Software Corporation. All Rights Reserved.