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

Replacing blank spaces using 'Replace' statement

0 Likes
6,879

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'

1 ACCEPTED SOLUTION
Read only

Former Member
2,618

Hi Jeba,

to do this use :

TRANSLATE str USING ' _'.   " there is a blank before the underscore ! 

It should work !

Erwan

9 REPLIES 9
Read only

Former Member
0 Likes
2,618

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

Read only

Former Member
0 Likes
2,618

hi there,

not sure if there´s a possibility, but i would then check for the ASCII-value of space which is 32.

Read only

Former Member
0 Likes
2,618
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.
Read only

Former Member
0 Likes
2,618

execute the code .

data:  str(15) type c value ' Award Notice'.
write:/ str.
replace ' '  with '_' into str.
write:/ str.

regards,

vijay

Read only

0 Likes
2,618

vijay, awesome !

Read only

Former Member
0 Likes
2,618

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

Read only

uwe_schieferstein
Active Contributor
0 Likes
2,618

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

Read only

Former Member
2,619

Hi Jeba,

to do this use :

TRANSLATE str USING ' _'.   " there is a blank before the underscore ! 

It should work !

Erwan

Read only

Former Member
0 Likes
2,618
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