08-27-2014 9:20 PM
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!
08-27-2014 10:49 PM
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
08-27-2014 10:49 PM
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
08-28-2014 3:59 AM
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
08-28-2014 6:30 PM
08-29-2014 7:14 AM
Here is one more way to do it. It should work fine for ABAP release 702 and above.
EDIT: sy-subrc check removed because it is not needed.
/.
Message was edited by: Manish Kumar
08-29-2014 2:35 PM
08-29-2014 3:41 PM