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: 

String character replacement manipulation

Former Member
0 Kudos

Hi, i'm new and have a question that I havn't seen answered on here yet.

I'm looking for a way to replace a string, ex: "You need # keyboard and # screens "

with something like " You need #1 keyboard and #2 screens".

Essentially, I need a way to loop through the string itself, and have an index to increase for each occurance of #, leaving a number after it.

The index increases by 1 for each occurance in the string, and resets when I put in a new string (i'm getting this string from a table value)

So make " There is # computer"

into "There is #1 computer"

and also "You need # keyboard and # screens "

into " You need #1 keyboard and #2 screens".

Thanks in advance!

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Chris,

I new too and i've made this, i don't know if this is the kind of solution that you are looking for but i hope it helps.

REPORT  z_teste_brn_string.

TYPES: BEGIN OF ty_stab,

   field1(50) TYPE c,

END OF ty_stab.

TYPES: BEGIN OF ty_field,

   field1(50) TYPE c,

END OF ty_field.

DATA: it_stab TYPE STANDARD TABLE OF ty_stab.

DATA: it_field TYPE STANDARD TABLE OF ty_field.

DATA: v_string  TYPE string,

       v_string2 TYPE string,

       v_count   TYPE n,

       v_lines   TYPE n,

       v_final   TYPE string.

FIELD-SYMBOLS: <fs_stab> TYPE ty_stab.

FIELD-SYMBOLS: <fs_field> TYPE ty_field.

v_string  = 'there is # computer'.

v_string2 = 'You need # keyboard and # screens'.

APPEND v_string TO it_stab.

APPEND v_string2 TO it_stab.

LOOP AT it_stab ASSIGNING <fs_stab>.

   SPLIT <fs_stab> AT '#' INTO TABLE it_field.

   DESCRIBE TABLE it_field LINES v_lines.

   LOOP AT it_field ASSIGNING <fs_field>.

     v_count = v_count + 1.

     IF v_lines NE v_count.

       CONCATENATE <fs_field> '#' v_count INTO <fs_field>.

       CONCATENATE v_final <fs_field> INTO v_final.

     ELSE.

       CONCATENATE v_final <fs_field> INTO v_final.

     ENDIF.

   ENDLOOP.

   <fs_stab> = v_final.

   CLEAR: v_count,

          v_final.

   BREAK-POINT.

ENDLOOP.


Regards,

Bruno Magalhães

6 REPLIES 6

Former Member
0 Kudos

Hi Chris,

I new too and i've made this, i don't know if this is the kind of solution that you are looking for but i hope it helps.

REPORT  z_teste_brn_string.

TYPES: BEGIN OF ty_stab,

   field1(50) TYPE c,

END OF ty_stab.

TYPES: BEGIN OF ty_field,

   field1(50) TYPE c,

END OF ty_field.

DATA: it_stab TYPE STANDARD TABLE OF ty_stab.

DATA: it_field TYPE STANDARD TABLE OF ty_field.

DATA: v_string  TYPE string,

       v_string2 TYPE string,

       v_count   TYPE n,

       v_lines   TYPE n,

       v_final   TYPE string.

FIELD-SYMBOLS: <fs_stab> TYPE ty_stab.

FIELD-SYMBOLS: <fs_field> TYPE ty_field.

v_string  = 'there is # computer'.

v_string2 = 'You need # keyboard and # screens'.

APPEND v_string TO it_stab.

APPEND v_string2 TO it_stab.

LOOP AT it_stab ASSIGNING <fs_stab>.

   SPLIT <fs_stab> AT '#' INTO TABLE it_field.

   DESCRIBE TABLE it_field LINES v_lines.

   LOOP AT it_field ASSIGNING <fs_field>.

     v_count = v_count + 1.

     IF v_lines NE v_count.

       CONCATENATE <fs_field> '#' v_count INTO <fs_field>.

       CONCATENATE v_final <fs_field> INTO v_final.

     ELSE.

       CONCATENATE v_final <fs_field> INTO v_final.

     ENDIF.

   ENDLOOP.

   <fs_stab> = v_final.

   CLEAR: v_count,

          v_final.

   BREAK-POINT.

ENDLOOP.


Regards,

Bruno Magalhães

Former Member
0 Kudos

Hi Chris,

Try the below code.

data: lv_hash(1) value '#',
        lv_spch(1) value '~',
        lv_cnt type i.
data: lv_char(2).


loop at itab.
replace all OCCURRENCES of lv_hash in itab-str with lv_spch replacement count lv_cnt.
do lv_cnt times.
lv_char = lv_hash && sy-index.
replace lv_spch in itab-str with lv_char.
enddo.

endloop.

Make sure the special character you use, will not be in the string.

Thanks,
Vamshi

Former Member
0 Kudos

Thanks!

Former Member
0 Kudos

Here is one more way to do it. It should work fine for ABAP release 702 and above.

  1. DATA: text TYPE string VALUE 'You need to count # # and #',
  2.       substring TYPE c VALUE '#'.
  3. DO count( val = text
  4.           sub = substring ) TIMES.
  5.   text = replace( val   = text
  6.                   sub   = substring
  7.                   with  = substring && sy-index
  8.                   occ   = sy-index ).
  9. ENDDO.
  10. WRITE text.

EDIT: sy-subrc check removed because it is not needed.

/.

Message was edited by: Manish Kumar

0 Kudos

Great solution!

0 Kudos

really impressive solution...

i'll record that! =P