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

How to use REPLACE ALL OCCURRENCES with space characters?

Former Member
124,243

HI all, I have a simple but interesting case:

  DATA: lv_name TYPE string VALUE 'this;is;a;test'.

  REPLACE ALL OCCURRENCES OF ';' IN lv_name WITH space.
  WRITE lv_name.

The strange thing is, this report's output is "thisisatest", but not "this is a test", as thought.

What is the problem here?

Kind regards, Matthias

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
39,911

HI,

Try this out,

DATA: lv_name(60) TYPE C..

  • VALUE 'this;is;a;test'.

lv_name = 'this;is;a;test'.

WHILE lv_name CS ';'.

lv_name+SY-FDPOS(1) = SPACE.

ENDWHILE.

WRITE lv_name.

Hope it helps you,

Regards,

Abhijit G. Borkar

12 REPLIES 12
Read only

Former Member
39,911

Hi Matthias,

I think this is explained in the ABAP help, but not very clearly.

It says, "In the case of character string processing, the closing spaces are taken into account for data objects dobj of fixed length; they are not taken into account in the case of new." Where dobj is ';' and new is space.

So, what it seems to be saying is, if there's a trailing space in the replaced string (';') it is considered. But if there is a trailing space in the replacing string (here space) it is removed. As your replacing string is entirely space, it removes it all.

I tried using '. .' (dot, space,dot) as the replacing string, and the space is retained, but if I use '. ' (dot, space) the space is removed.

I guess a potential work around is to do this is two steps, replace ';' with ' .', then replace '.' with space.

Regards,

Nick

Read only

Former Member
0 Likes
39,911

Hi Matthias,

I have faced same issue when i was working on a report. You can't use 'replace all' to insert spaces in a string.

You can achieve it using 2 ways:

Split your string at ';' and save it in different variables. And concatenate these variables in a string separated by space. So that you will be able to display your string as 'this is a test'.

Or you can use 'translate' to replace ';' with space. I am not sure whether this option will work. You can just try it out.

Thanks,

Archana

Read only

Former Member
0 Likes
39,912

HI,

Try this out,

DATA: lv_name(60) TYPE C..

  • VALUE 'this;is;a;test'.

lv_name = 'this;is;a;test'.

WHILE lv_name CS ';'.

lv_name+SY-FDPOS(1) = SPACE.

ENDWHILE.

WRITE lv_name.

Hope it helps you,

Regards,

Abhijit G. Borkar

Read only

0 Likes
39,911

Hi,

another simple way is to use TRANSLATE:


TRANSLATE lv_name USING '; '.

Regards,

Frisoni

Read only

39,911

Thanks for all you replies.

It seems the nicest solution is

TRANSLATE lv_name USING '; '.

Kind regards, Matthias

Read only

former_member480923
Active Contributor
0 Likes
39,911

hi don't have a idea why ur code is not working but this one is working

DATA: lv_name TYPE string VALUE 'this;is;a;test'.

  DO.
    REPLACE ';' WITH space INTO lv_name.
    IF sy-subrc NE 0.
      EXIT.
    ENDIF.
  ENDDO.
  WRITE lv_name.

Hope That Helps

Anirban M.

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
39,911

Try

replace ',' with space into lv_name.

WRITE: / lv_name.

Read only

Clemenss
Active Contributor
39,911

Hi,

OK TRANSLATE is the smartest solution, but

REPLACE ALL OCCURENCES OF ';' IN lv_name WITH ` `.

works as well - note the special string delimiter ` not '.

Regards,

Clemens

Read only

39,911

I think this is the best solution!

Read only

0 Likes
39,911

Perfect solution!!

Read only

NemanjaSimovic
Participant
39,911

Vertical bar forces ABAP to respect space. 😉 Works 100%.

REPLACE ALL OCCURRENCES OF ';' IN lv_name WITH | |.


Use vertical bars in your code always when you want to respect the spaces.

Regards,

Nemanja

Read only

0 Likes
39,911

This is so easy and brilliant it works. Thank you .