Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

String operation

Anindya
Explorer
0 Likes
798

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

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
766

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

6 REPLIES 6
Read only

Former Member
0 Likes
767

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

Read only

Former Member
0 Likes
766

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.

Read only

Former Member
0 Likes
766

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.

Read only

Former Member
0 Likes
766

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.

Read only

Former Member
0 Likes
766

Check the below FM. It helps u...

NUMERIC_CHECK

Read only

Former Member
0 Likes
766

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 number

i/p: 12g8#1?5
o/p: 12815

Hope this helps you.

Regards,

Manoj Kumar P

Edited by: Manoj Kumar on Jan 13, 2009 9:42 AM