Simplify???

whwar9739

Member
Does anyone know of a simple way to check for a number in a string?

Basically trying to simplify the following

Code:
IF l-string MATCHES "*1*"
OR                  "*2*"
...
OR                  "*0*"
 
Code:
function isNumeric returns logical ( input string as character ):

  define variable x as decimal.

  assign
    x = decimal( string )
  no-error.

  return not error-status:error.

end.

display isNumeric( "1234" ).
pause.

display isNumeric( "xyz" ).
pause.

display isNumeric( "abc123" ).
pause.
 
The only issue there is that the line may contain non numeric values as well. Something similar to an address.

1234 Anywhere St
 
Oops! I didn't read that correctly and thus answered a different question. Sorry :(

You want to know if there is a digit anywhere in a string?

Code:
define variable i as integer no-undo.
define variable s as character no-undo.

s = "abc7xyz".

do i = 0 to 9:
  if index( s, chr( i + 48 )) > 0 then
    do:
      message "Yes!".
      leave.
    end.
end.
 
... or if you want to see if a string contains a number within (using Tom's as a starting point):

Code:
function hasNumber returns logical ( input instring as character ):

  define variable i as integer.

  do i = 0 to 9:
    if index(instring,string(i)) > 0
    then return true.
  end.

  return false.

end.

display hasNumber( "1234" ).
pause.

display hasNumber( "xyz" ).
pause.

display hasNumber( "abc123" ).
pause.
EDIT: Sorry Tom, I was posting this as you were. Either method would work.
 
Substring the string, checking the ASCII value. That way you will know if it is what you need to know if you are interested.
 
I know this has been answered but I decided to write a recursive solution...

Code:
DEFINE VARIABLE lvNums AS CHARACTER   NO-UNDO INITIAL "0,1,2,3,4,5,6,7,8,9".


FUNCTION HasNumber RETURNS LOGICAL 
  (INPUT ipChar AS CHARACTER):

  DEFINE VARIABLE lvFirstChar AS CHARACTER   NO-UNDO.
  DEFINE VARIABLE lvRest AS CHARACTER   NO-UNDO.

  ASSIGN
    lvFirstChar = SUBSTRING(ipChar,1,1)
    lvRest      = SUBSTRING(ipChar,2).

  IF LOOKUP(lvFirstChar,lvNums) GT 1 THEN
    RETURN TRUE. 

  IF lvRest EQ "" THEN
    RETURN FALSE.

  RETURN HasNumber(lvRest).


END FUNCTION.




MESSAGE HasNumber( "1234" )
  VIEW-AS ALERT-BOX INFO BUTTONS OK.
MESSAGE HasNumber( "xyz" )
  VIEW-AS ALERT-BOX INFO BUTTONS OK.
MESSAGE HasNumber( "abc123" )
  VIEW-AS ALERT-BOX INFO BUTTONS OK.
 
Back
Top