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

Write a string separated by Hyphens.

0 Likes
2,754

I have a string,

lv_serial = 'ABCDEFGHIJKL1237'.

Want to write like this below,

'AB-CD-EF-GH-IJ-KL-12-37'.

Need help.

8 REPLIES 8
Read only

FredericGirod
Active Contributor
2,247

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 -

Read only

0 Likes
2,247

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 '-'.

Read only

0 Likes
2,247

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.

Read only

0 Likes
2,247

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 '-'.
Read only

2,247

Gábor Márián The pain of people who don't know REGEX or REGEX within ABAP 😉

Read only

0 Likes
2,247

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 !

Read only

Sandra_Rossi
Active Contributor
2,247

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.

Read only

dev_parbutteea
Active Contributor
2,247
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 .