‎2020 Jan 10 9:46 AM
I have a string,
lv_serial = 'ABCDEFGHIJKL1237'.
Want to write like this below,
'AB-CD-EF-GH-IJ-KL-12-37'.
Need help.
‎2020 Jan 10 10:05 AM
As it is a string and you do not know the size of the input, you cannot use REGEX, WRITE ... MASK, CONCATENATE, ...
You need to do a loop in the character of the input and every 2 insert the character -
‎2020 Jan 10 10:37 AM
Hi frdric.girod,
I have written a code for this. But need to reduce garbage code. Complexity level is high.
lv_serial = 'ABCDEFGHIJKL1237'.
lv_pos = strlen( lv_serial ).
DO lv_pos TIMES.
IF sy-index = 2.
MOVE lv_serial+0(2) TO lv_serial2.
ELSEIF sy-index = 4.
MOVE lv_serial+2(2) TO lv_serial3.
ELSEIF sy-index = 6.
MOVE lv_serial+4(2) TO lv_serial4.
ELSEIF sy-index = 8.
MOVE lv_serial+6(2) TO lv_serial5.
ELSEIF sy-index = 10.
MOVE lv_serial+8(2) TO lv_serial6.
ELSEIF sy-index = 12.
MOVE lv_serial+10(2) TO lv_serial7.
ELSEIF sy-index = 14.
MOVE lv_serial+12(2) TO lv_serial8.
ELSEIF sy-index = 16.
MOVE lv_serial+14(2) TO lv_serial9.
ENDIF.
ENDDO.
CONCATENATE lv_serial2 lv_serial3 lv_serial4 lv_serial5 lv_serial6 lv_serial7 lv_serial8 lv_serial9 INTO lv_serial10 SEPARATED BY '-'.
‎2020 Jan 10 10:48 AM
Even if you don't know the size, you can do it with a REGEX but it's not worth the pain for a so simple algorithm.
‎2020 Jan 10 11:27 AM
sandra.rossi - I don't think it is that much of pain if the parts have the same size
matcher = cl_abap_matcher=>create( pattern = '([A-Za-z0-9]{2})' text = serial ).
WHILE matcher->find_next( ).
INSERT matcher->get_submatch( 0 ) INTO TABLE tokens.
ENDWHILE.
CONCATENATE LINES OF tokens INTO result SEPARATED BY '-'.
‎2020 Jan 10 11:41 AM
Gábor Márián The pain of people who don't know REGEX or REGEX within ABAP 😉
‎2020 Jan 10 12:25 PM
Gábor Márián you should post your proposal as an answer
I was searching for REGEX to replace directly, not imagining this solution. Good idea !
‎2020 Jan 10 10:14 AM
I think that the goal of the exercise is to let you search in the ABAP documentation and find the right functions. For instance, start here: string expressions and functions.
‎2020 Jan 10 1:33 PM
DATA:gv_string TYPE string VALUE 'ABCDEFGHIJKLMNOPQR',
gv_string_final TYPE string,
lv_mod TYPE i,
gv_length TYPE i,
gv_offset TYPE i.
gv_length = strlen( gv_string ).
DO gv_length TIMES.
CONCATENATE gv_string_final gv_string+gv_offset(1) INTO gv_string_final.
lv_mod = gv_offset MOD 2.
IF sy-index = gv_length. "exit on reaching last caracter
EXIT.
ENDIF.
IF lv_mod = 1.
CONCATENATE gv_string_final '-' INTO gv_string_final.
ENDIF.
gv_offset = gv_offset + 1.
ENDDO.
WRITE:/ 'Input string: ', gv_string .
WRITE:/ 'Result: ', gv_string_final .