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: 

Replacing blank spaces using 'Replace' statement

0 Kudos

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

Former Member

Hi Jeba,

to do this use :

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

It should work !

Erwan

9 REPLIES 9

Former Member
0 Kudos

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

Former Member
0 Kudos

hi there,

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

Former Member
0 Kudos
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.

Former Member
0 Kudos

execute the code .

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

regards,

vijay

0 Kudos

vijay, awesome !

former_member1259992
Participant
0 Kudos

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

uwe_schieferstein
Active Contributor
0 Kudos

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

Former Member

Hi Jeba,

to do this use :

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

It should work !

Erwan

Former Member
0 Kudos
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