Application Development 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: 

How to use REPLACE ALL OCCURRENCES with space characters?

Former Member
94,538

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

Former Member
0 Kudos
10,206

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

Former Member
10,206

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

Former Member
0 Kudos
10,206

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

Former Member
0 Kudos
10,207

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

0 Kudos
10,206

Hi,

another simple way is to use TRANSLATE:


TRANSLATE lv_name USING '; '.

Regards,

Frisoni

10,206

Thanks for all you replies.

It seems the nicest solution is

TRANSLATE lv_name USING '; '.

Kind regards, Matthias

former_member480923
Active Contributor
0 Kudos
10,206

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.

kesavadas_thekkillath
Active Contributor
0 Kudos
10,206

Try

replace ',' with space into lv_name.

WRITE: / lv_name.

Clemenss
Active Contributor
10,206

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

10,206

I think this is the best solution!

0 Kudos
10,206

Perfect solution!!

NemanjaSimovic
Participant
10,206

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

0 Kudos
10,206

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