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

Replacing multiple new line characters

Former Member
0 Likes
4,811

how to replace multiple new line characters in a line.

Suppose for example my line contains Example1#####Example2##Example3###Example4,

then the output should be Example1#Example2#Example3#Example4.

6 REPLIES 6
Read only

Former Member
0 Likes
2,327

Hi Sid,

You Can try this code

DATA: ld_string TYPE string,

ld_string1 TYPE string,

ld_string2 TYPE string,

ld_string3 TYPE string.

ld_string = 'Example1#####Example2##Example3###Example4'.

REPLACE ALL OCCURRENCES OF '######' in ld_string with '#'.

REPLACE ALL OCCURRENCES OF '####' in ld_string with '#'.

REPLACE ALL OCCURRENCES OF '###' in ld_string with '#'.

REPLACE ALL OCCURRENCES OF '##' in ld_string with '#'.

write:/1 ld_string.

Regards

Pavan

Read only

rainer_hbenthal
Active Contributor
0 Likes
2,327

REPLACE ALL OCCURRENCES OF cl_abap_char_utilites=>new_line in ld_string with ``.
Read only

0 Likes
2,327

Sorry my first attempt was nonsens.


  concatenate CL_ABAP_CHAR_UTILITIES=>newline '+' into pattern.
  replace all OCCURRENCES OF regex pattern in s with CL_ABAP_CHAR_UTILITIES=>newline.

Read only

Former Member
0 Likes
2,327

Hi Sidhartha,

Please make use the below code to solve your issue.

**************************************************************

DATA: v_line(50) type c value'Example1#####Example2##Example3###Example4',

begin of itab occurs 0,

data(25) type c,

end of itab.

split v_line at '#' into table itab.

clear v_line.

loop at itab where data is not initial.

concatenate v_line itab-data into v_line separated by '#'.

endloop.

if v_line+0(1) = '#'.

v_line+0(1) = ''.

endif.

condense v_line no-gaps.

write : / v_line.

**********************************************************

Hope this will help you.

Regards,

Smart Varghese

Read only

Former Member
0 Likes
2,327

Hi Sidhartha,

Try this piece of code -

REPORT sy-repid.

TYPES: BEGIN OF ty_split,
        str_lines(255),
       END OF ty_split.

DATA: str TYPE string,
      itab_split TYPE TABLE OF ty_split,
      wa_split TYPE ty_split.

* str contains your input string which has new line characters. you may put the value in that variable as per your logic.

SPLIT str AT cl_abap_char_utilities=>cr_lf INTO TABLE itab_split.

CLEAR str.
LOOP AT itab_split INTO wa_split.
  IF sy-tabix EQ 1.
    str = wa_split-str_lines.
  ELSE.
    CONCATENATE str cl_abap_char_utilities=>newline wa_split-str_lines  INTO str.
  ENDIF.
ENDLOOP.

WRITE str.

Hope this helps.

Regards,

Himanshu

Read only

Former Member
0 Likes
2,327

Thanks I am able to solve the problem of multi line replacement.