Skip to Content
author's profile photo Former Member
Former Member

How to use REPLACE ALL OCCURRENCES with space characters?

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

Add comment
10|10000 characters needed characters exceeded

6 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Jan 12, 2010 at 11:07 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Posted on Jan 12, 2010 at 02:39 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 12, 2010 at 11:05 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Posted on Jan 12, 2010 at 11:26 AM

    Try

    replace ',' with space into lv_name.

    WRITE: / lv_name.

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 12, 2010 at 11:06 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 12, 2010 at 11:21 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded