Application Development 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: 

Looping through a string and check for Number

Former Member
0 Kudos

Hello,

I want to check whether each of the character of a string is numeric. If all numeric then assign each of the numeric character to a variable.

e.g.

string = '123456789'

loop through the string
  if nth character of the string is numeric
    n1 = nth character
  elseif any one of the character is not number then
    error message
  end
end loop

Please help me out.

Kind regards.

6 REPLIES 6

Former Member

No need to loop the string.

we can write the code as following.

string = '123456789'

if string co '0123456789'.

write:/...

else.

;;

endif.

data: str(100) type c.
data: str_n type string.
data: str_c type string.
data: str_d type string.
data: str_p type string.
 
data: len1 type i.
data: ofst1 type i.
 
str = '#ABCD%123'.
len1 = strlen( str ).
 
 
 
do.
 
  if ofst1 = len1.
    exit.
  endif.
 
  if str+ofst1(1) co sy-abcde  .
      concatenate str_c str+ofst1(1) into str_c.
  elseif str+ofst1(1) co '0123456789'  .
    concatenate str_p str+ofst1(1) into str_p.
    else.
    concatenate str_d str+ofst1(1) into str_d.
    endif.
 
  ofst1 = ofst1 + 1.
 
enddo.
 
 
write:/ str.
write:/ 'alphabatic char', 20 str_c.
 
write:/ 'spacial character', 20 str_d.
write:/ 'numeric char', 20 str_p.

Former Member
0 Kudos

Hi Nazmul,

I hope that this code will works.

constants:
   c_digit_grp        TYPE char11         VALUE '0123456789',        " Digit group

* Data Declaration
data : 
  str   type string,
  l1    type i,
  count type i.

* Structure Declartion
data: begin of wa_itab,
      cha  type char01,
      end of wa_itab.

* Table declartion
data: t_tab type table of wa_itab.



* if you want check entire string and pass entire string
if  str CN c_digit_grp.
    Message 'Error' Type 'E'.
else.
    N1 = str.                       " N1 is numeric character
Endif.

* if you want to check entire string and pass each numeric character to a variable
if  str CN c_digit_grp.
    Message 'Error' Type 'E'.
else.
     l1 = strlen ( str ).
    do l1 times.
     count  = count + 1.
     wa_itab-cha = string+count(1).
     append wa_itab to t_tab.
    enddo.
endif.

Regards

Bhupal

Message was edited by:

Bhupal Reddy Vendidandi

0 Kudos

It is almost close what I wanted.

I'm getting the string value from a form field and want to make sure user enters only numbers.

If user entered all numbers then I need to assign each of these numbers to variables.

e.g. User entered numbers '123456789' will be assigned n1 = 1, n2 = 2, n3 = 3, n4 = 4, n5 = 5 etc, I need to do some calculations with this numbers to get a checksum.

Do I realy need wa_itab to do this? In the loop can't I just go through each of these number and assigned to variables n1, n2. etc?

Thanks

Naz

Former Member
0 Kudos

Hi,

check this code

DATA: string(15) TYPE c VALUE '012345N789',

len TYPE i,

offset TYPE i,

n(1) TYPE n,

n1 TYPE TABLE OF n WITH HEADER LINE.

DESCRIBE FIELD string LENGTH len IN CHARACTER MODE.

DO len TIMES.

offset = sy-index - 1.

CHECK string+offset(1) <> space.

IF string+offset(1) CA '0123456789'.

MOVE string+offset(1) TO n1.

APPEND n1.

ELSE.

WRITE : string+offset(1), 'is not a number'.

ENDIF.

ENDDO.

Regards,

Kasi S

Former Member
0 Kudos

Thanks a lot guys.