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

Split string question - split at non-numeric character

Former Member
0 Likes
1,249

I have a string that is in the form of ##R# (# = any number from 0-9 and the quantity of numbers is dynamic). How can I split the string at the first non-numeric character? Below is an example:

String = 12R3

I need var1 = '12' and var2 = 'R3'.

Regards,

Davis

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
742

I know, not the most elegant solution, but a solution non-the-less.



REPORT  rich_0001.


DATA: lv_str TYPE string VALUE '123456R987654321'.
DATA: lv_var1 TYPE string.
DATA: lv_var2 TYPE string.
DATA: lv_counter TYPE i.
data: lv_offset type i.
data: lv_len type i.


DO.
  IF lv_str+lv_counter(1) NA '0123456789'.
      lv_offset = lv_counter.
    exit.
  ENDIF.
  lv_counter = lv_counter + 1.
ENDDO.

lv_var1 = lv_str(lv_offset).
lv_len = strlen( lv_str ).
lv_offset = lv_offset + 1.  " Skip over the non-numeric
lv_var2 = lv_str+lv_offset.

write:/ lv_var1.
write:/ lv_var2.



Regards,

Rich Heilman

3 REPLIES 3
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
743

I know, not the most elegant solution, but a solution non-the-less.



REPORT  rich_0001.


DATA: lv_str TYPE string VALUE '123456R987654321'.
DATA: lv_var1 TYPE string.
DATA: lv_var2 TYPE string.
DATA: lv_counter TYPE i.
data: lv_offset type i.
data: lv_len type i.


DO.
  IF lv_str+lv_counter(1) NA '0123456789'.
      lv_offset = lv_counter.
    exit.
  ENDIF.
  lv_counter = lv_counter + 1.
ENDDO.

lv_var1 = lv_str(lv_offset).
lv_len = strlen( lv_str ).
lv_offset = lv_offset + 1.  " Skip over the non-numeric
lv_var2 = lv_str+lv_offset.

write:/ lv_var1.
write:/ lv_var2.



Regards,

Rich Heilman

Read only

0 Likes
742

Rich,

Thanks a lot for that piece of code! I was hoping that I could do something like:

split string at (0 1 2 3 4 5 6 7 8 9) into var

but I guess that isn't possible.

Thanks again,

Davis

Read only

gopi_narendra
Active Contributor
0 Likes
742

Check this code.


PARAMETERS : p_c(10) TYPE c.
DATA : l_pos TYPE i.
DATA : l_pos1 TYPE i.
DATA : p_c1(10) TYPE c.
DATA : p_c2(10) TYPE c.

IF p_c CA sy-abcde.
  l_pos = sy-fdpos.
  l_pos1 = l_pos + 1.
ENDIF.

WRITE : p_c+0(l_pos).
WRITE : p_c+l_pos1.

Regards

Gopi