‎2009 Jan 13 7:49 AM
Hi All,
I have a string variable which should ideally contain an integer value which i am asigning to an integer variable. Now accidentaly if a character value is present in the string variable the program gives a dump while trying to assign that character variable to the integer variable.
So here before i assign the string variable(which should ideally contain an integer value) to the integer variable i need to perform a check, to check whether the string variable contains an integer value or not.
How can i do this? What can be the best possible logic to detect this???
Thanks in advance,
Anindya
‎2009 Jan 13 7:53 AM
try this
Check string_value CO '1234567890' " to be read as the string_value contains only characters from the string 1234567890 . i.e. it only contains numbers
regards,
Advait
‎2009 Jan 13 7:53 AM
try this
Check string_value CO '1234567890' " to be read as the string_value contains only characters from the string 1234567890 . i.e. it only contains numbers
regards,
Advait
‎2009 Jan 13 7:54 AM
use if variable CA 'ABCDEFG'.
ENDIF.
IF Variable contains value as Text.
the if condition will succeed and goes inside it, then we can say the variable containing character instead of integer.
‎2009 Jan 13 7:59 AM
try with string CO '0123456789'.
or do exception handling.
data OREF type ref to CX_ROOT.
data TEXT type STRING.
try.
int = string.
CATCH CX_SY_CONVERSION_NO_NUMBER INTO OREF.
TEXT = OREF->GET_TEXT( ).
CLEANUP.
CLEAR int.
endtry.
if not TEXT is initial.
MESSAGE e001(00) WITH text. "error
endif.
‎2009 Jan 13 8:11 AM
Check this snippet. Hope will be helpful for you.
DATA a TYPE string VALUE '234'.
DATA b TYPE i.
IF a CO '0123456789'.
b = a.
WRITE b.
ELSE.
MESSAGE 'Character is present in string' TYPE 'I'.
ENDIF.
‎2009 Jan 13 8:18 AM
‎2009 Jan 13 8:37 AM
Hi Anindya,
Check this code, which removes all characters other than numbers in a string
and assigns it to an integer.
DATA: w_txt TYPE string,
w_hold TYPE string.
DATA w_num TYPE i.
w_txt = '12g8#1?5'.
DO.
IF w_txt CO '0123456789'.
EXIT.
ELSE.
w_hold = w_txt+sy-fdpos(1).
REPLACE w_hold WITH space INTO w_txt.
CONDENSE w_txt NO-GAPS.
ENDIF.
ENDDO.
w_num = w_txt. "Required numberi/p: 12g8#1?5
o/p: 12815Hope this helps you.
Regards,
Manoj Kumar P
Edited by: Manoj Kumar on Jan 13, 2009 9:42 AM