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

replace

Former Member
0 Likes
1,422

Hye Gurus,

i have a variable of type char40.

when i am using

replace all occurences of space in var with '_'.

it is throwing an exception.

Please suggest, my requirement is i have to replace spaces with underscore '_'.

Thanks in advance,

Imran.

1 ACCEPTED SOLUTION
Read only

JozsefSzikszai
Active Contributor
0 Likes
1,392

hi,

you can do with TRANSLATE:

TRANSLATE string USING ' _'.

(pls note there is a space before the undescore)

hope this helps

ec

10 REPLIES 10
Read only

JozsefSzikszai
Active Contributor
0 Likes
1,393

hi,

you can do with TRANSLATE:

TRANSLATE string USING ' _'.

(pls note there is a space before the undescore)

hope this helps

ec

Read only

former_member188827
Active Contributor
0 Likes
1,392

what is the exception text?

Read only

Former Member
0 Likes
1,392

Hi Imran,

Try these examples. :

Addition 1

... FIRST OCCURRENCE OF

Effect

In the string text, replaces the first occurrence of the search text old with new. This addition is also the default setting.

Example

DATA: myText type string.

myText = 'abcabcabcabcabc'.

REPLACE FIRST OCCURRENCE OF 'abc' IN myText WITH 'XYZ'.

returns: myText = 'XYZabcabcabcabc', sy-subrc = 0

Addition 2

... ALL OCCURRENCES OF

Effect

In the string text, replaces all patterns found that do not overlap. Bear in mind that the system searches the entire string and then replaces old with new.

Example 1

DATA: c4(4) TYPE C.

c4 = 'abab'.

REPLACE ALL OCCURRENCES OF 'ab' IN c4 WITH 'CCC'.

returns: c4 = 'CCCC', sy-subrc = 2

not c4 = 'CCCa', sy-subrc = 2. (as it would, if the system replaced each search string successively).

Example 2

DATA: myText type string.

myText = 'abcabcabcabcabc'.

REPLACE ALL OCCURRENCES OF 'abc' IN myText WITH 'XYZ'.

returns: myText = 'XYZXYZXYZXYZXYZ', sy-subrc = 0

Regards,

Swapna

Read only

Former Member
0 Likes
1,392

You can write:


data:
  zlv_char type char40.

translate zlv_char using ' _'.

Sorry, I see that Eric already gave the correct answer.

Regards,

John.

Edited by: John Heutmekers on Jun 30, 2008 12:35 PM

Read only

Former Member
0 Likes
1,392

Hi,

Try using translate

TRANSLATE <variablename> USING ' _' .

Regards,

Sai

Read only

sachin_mathapati
Contributor
0 Likes
1,392

hi Imran,

Try translate Instead of Replace.

Regards,

Sachin M M

Read only

Former Member
0 Likes
1,392

Hi,

use this.. REPLACE ALL OCCURRENCES OF ' ' IN wa_final-last_name WITH '_'.

Read only

former_member188827
Active Contributor
0 Likes
1,392

try:

data str(50) VALUE 'abc def ghi'.

data: in type i, ind TYPE i.

in = STRLEN( str ).

WHILE ind lt in.

if str+ind(1) = ''.

move '_' to str+ind(1).

endif.

ind = ind + 1.

ENDWHILE.

WRITE / str.

plz reward points if dis helps

Read only

Former Member
0 Likes
1,392

This message was moderated.

Read only

Former Member
0 Likes
1,392

Hi imran ,

Try this once,

data:

w_char1(40) type c,

w_c type c value space,

w_char(40) type c ,

w_c1 type c value '_',

w_new type c.

w_char = ' hello hai how r u'.

condense w_char.

while w_char ne ' '.

w_new = w_char(1).

shift w_char left.

if w_new eq space.

replace w_new in w_char with w_c1.

endif.

concatenate w_char1 w_new into w_char1.

endwhile.

write w_char1.

Regards,

Rajitha.