Find Number in a String....

i have this string:
Code:
(test testest 123456789)

the number of words is diffrent each time...

so i want to get the number out of this string so that the string ist:

Code:
123456789

has someone an idea?
 

rstanciu

Member
something like this ...

DEFINE VARIABLE lc AS CHARACTER NO-UNDO.
DEFINE VARIABLE liCnt AS INTEGER NO-UNDO.
DEFINE VARIABLE li AS INTEGER NO-UNDO.

lc = "test testest 123456789".
DO liCnt = 1 TO NUM-ENTRIES(lc, " "):
ASSIGN
li = INTEGER(ENTRY(liCnt,lc," ")) NO-ERROR.
IF ERROR-STATUS: ERROR THEN NEXT.
ELSE LEAVE.
END.
MESSAGE li VIEW-AS ALERT-BOX.
 
something like this ...

DEFINE VARIABLE lc AS CHARACTER NO-UNDO.
DEFINE VARIABLE liCnt AS INTEGER NO-UNDO.
DEFINE VARIABLE li AS INTEGER NO-UNDO.

lc = "test testest 123456789".
DO liCnt = 1 TO NUM-ENTRIES(lc, " "):
ASSIGN
li = INTEGER(ENTRY(liCnt,lc," ")) NO-ERROR.
IF ERROR-STATUS: ERROR THEN NEXT.
ELSE LEAVE.
END.
MESSAGE li VIEW-AS ALERT-BOX.

thank you, works fine :D
 

karthikeyan

New Member
i have this string:
Code:
(test testest 123456789)
the number of words is diffrent each time...

so i want to get the number out of this string so that the string ist:

Code:
123456789
has someone an idea?



Try this


DEFINE VARIABLE m_char AS CHARACTER NO-UNDO INIT "test testest 123456789".
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE list AS CHARACTER NO-UNDO INIT "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z, ".
DEFINE VARIABLE m_final AS CHARACTER NO-UNDO.

REPEAT i = 1 TO LENGTH(m_char):
IF LOOKUP(SUBSTRING(m_char,i,1) , list) = 0 THEN
m_final = m_final + SUBSTRING(m_char,i,1).
END.

MESSAGE m_final VIEW-AS ALERT-BOX INFO BUTTONS OK.
 

palthe

Member
Try this


DEFINE VARIABLE m_char AS CHARACTER NO-UNDO INIT "test testest 123456789".
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE list AS CHARACTER NO-UNDO INIT "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z, ".
DEFINE VARIABLE m_final AS CHARACTER NO-UNDO.

REPEAT i = 1 TO LENGTH(m_char):
IF LOOKUP(SUBSTRING(m_char,i,1) , list) = 0 THEN
m_final = m_final + SUBSTRING(m_char,i,1).
END.

MESSAGE m_final VIEW-AS ALERT-BOX INFO BUTTONS OK.


Yes this is fine code to extract ALL the numbers out of a string. Be careful to use this, for it seems the poster wants to extract a number from a string which seems to be divided in clear fragments. So yes, this code can be used if the poster wants to get "14568" from the following string:
"tes1t 45test 6te8st".
 

TomBascom

Curmudgeon
A lot depends on the rules for recognizing numbers.

Is the string organized into "words" that are delimited in a known way? (perhaps by spaces) If so and if "number" meets the criteria for a Progress "decimal" then the following would work well:

Code:
define variable testString as character no-undo initial
  "test xyz 1234".
define variable i as integer no-undo.
define variable n as decimal no-undo.

do i = 1 to num-entries( testString, " " ):
  n = ?.
  assign n = decimal( entry( i, testString, " " )) no-error.
  if n <> ? then leave.
end.

display n.
 
Top