01-23-2007 1:26 PM
How to replace blank spaces in a sentence with underscore?
When I use : <b>Replace space in str with '_' .</b>
This statement inserts underscore in front of the string even though there is no space in the beginning or end of the string.
e.g.. If str is 'Award Notice'
Result will be '_Award Notice'
When I use: <b>Replace all occurrences of space in str with '_' .</b>
It gives a short dump: 'Endless loop'
01-23-2007 2:16 PM
Hi Jeba,
to do this use :
TRANSLATE str USING ' _'. " there is a blank before the underscore !
It should work !
Erwan
01-23-2007 1:33 PM
Hello,
U can do like this:
Data: str type string value 'Award Notice',
str1 like str,
str1 like str.
SPLIT str at space into str1 str2.
concatenate str1 str2 into str seperated by '_'.
write: str.
Hope this will solve ur problem.
Vasanth
01-23-2007 1:33 PM
hi there,
not sure if there´s a possibility, but i would then check for the ASCII-value of space which is 32.
01-23-2007 1:35 PM
REPORT ychatest LINE-COUNT 50.
DATA : v_str(15),
v1 TYPE i value 0,
len TYPE i.
v_str = 'Award Notice'.
len = STRLEN( v_str ).
DO len TIMES.
IF v_str+v1(1) EQ space.
move '_' to v_str+v1(1).
ENDIF.
v1 = v1 + 1.
ENDDO.
write : v_str.
01-23-2007 1:35 PM
execute the code .
data: str(15) type c value ' Award Notice'.
write:/ str.
replace ' ' with '_' into str.
write:/ str.
regards,
vijay
01-23-2007 1:36 PM
01-23-2007 1:39 PM
Hi Jeba,
Try TRANSLATE str USING ' _'.
If in doubt, type translate in your program, click on iot and hit F1 for the help.
Point if it helps please!!
Best Regards
Robin
01-23-2007 2:01 PM
Hello Jeba
The REPLACEMENT statement will not work (see documentation of ABAP statement). However, you could user either <b>OVERLAY</b> or <b>SPLIT</b> for replacing the spaces with any other sign.
*&---------------------------------------------------------------------*
*& Report ZUS_SDN_REPLACE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zus_sdn_replace.
DATA:
gd_string TYPE string,
gd_string_sav TYPE string,
gd_string_new TYPE string,
gt_split TYPE TABLE OF string.
START-OF-SELECTION.
gd_string = 'Award notice for me!'.
WRITE: / gd_string.
* NOTE: replace does not work -> see documentation of ABAP statement:
*Exceptions
*Catchable Exceptions
*
*
*
*CX_SY_RANGE_OUT_OF_BOUNDS
*
*Cause: Offset or length specification violated the bounds of character
*string dobj.
*Runtime Error: REFI_WRONG_SECTION (catchable)
* NOTE: works but "overlay" string must be sufficient long!
* overlay gd_string with '_______________________'.
* write: / gd_string.
SPLIT gd_string AT ' ' INTO TABLE gt_split.
LOOP AT gt_split INTO gd_string.
gd_string_sav = gd_string.
AT FIRST.
gd_string_new = gd_string_sav.
CONTINUE.
ENDAT.
AT LAST.
CONCATENATE gd_string_new gd_string_sav INTO gd_string_new.
CONTINUE.
ENDAT.
CONCATENATE gd_string_new gd_string_sav INTO gd_string_new
SEPARATED BY '_'.
ENDLOOP.
WRITE: / gd_string_new.
END-OF-SELECTION.
Regards
Uwe
01-23-2007 2:16 PM
Hi Jeba,
to do this use :
TRANSLATE str USING ' _'. " there is a blank before the underscore !
It should work !
Erwan
01-23-2007 2:32 PM
data: val(30) type c.
val = ' Award Notice'.
translate val using ' _'.
write:/ val .
OR
DATA : val(30) type c.
data : final(30) type c,
cnt type i,
v type c,
n type i.
val = ' AWARD NOTICE'.
cnt = strlen( val ).
do cnt times.
move val+n(1) to v.
if v EQ ' '.
move '_' to final+n(1).
ELSE.
move V to final+n(1).
endif.
N = N + 1.
enddo.
write:/ final .
regards,
vijay