01-12-2010 10:44 AM
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
01-12-2010 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
01-12-2010 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
01-12-2010 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
01-12-2010 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
01-12-2010 11:10 AM
Hi,
another simple way is to use TRANSLATE:
TRANSLATE lv_name USING '; '.
Regards,
Frisoni
01-12-2010 11:28 AM
Thanks for all you replies.
It seems the nicest solution is
TRANSLATE lv_name USING '; '.
Kind regards, Matthias
01-12-2010 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.
01-12-2010 11:26 AM
01-12-2010 2: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
07-25-2013 11:41 PM
05-12-2022 7:38 AM
09-09-2021 6:14 PM
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
02-16-2023 5:43 PM