Consultor Eletrônico



Kbase P148549: How to determine if a character in a string is an integer
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   14/07/2009
Status: Verified

GOAL:

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

GOAL:

How to find out which individual characters in a string are numbers

GOAL:

Using the ASC function to determine if a character in a string is INTEGER

FACT(s) (Environment):

All Supported Operating Systems
Progress 8.x
Progress 9.x
OpenEdge 10.x

FIX:

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 ):
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.