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

Code for replace

Former Member
0 Likes
1,945

Hi ,

I am trying to replace a character of the field from the itab.

itab-f1 = 'HOPT & SCHULER GMBH & CO KG'.

My code is like this:

loop at itab.

tmp = itab-f1.

IF lf_tmp CS '&'.

REPLACE '&' WITH '&' INTO lf_tmp.

ENDIF.

itab-f1 = tmp.

endloop.

I am able to change only the first '&' by doing this.

<b>HOPT &amp; SCHULER GMBH & CO K</b>

How can I do both the '&'?

anyone help me out ASAP

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,864

Hi,

Okay..You can use the Function module WRB_UTIL_STRING_REPLACE

Try this code

DATA: v_string TYPE string.

v_string = 'HOPT & SCHULER GMBH & CO KG'.

CALL FUNCTION 'WRB_UTIL_STRING_REPLACE'

EXPORTING

pi_search_string = '&'

pi_replace_string = '''&'''

CHANGING

pc_string = v_string

EXCEPTIONS

search_string_too_long = 1

missing_search_string = 2

OTHERS = 3.

IF sy-subrc = 0.

WRITE: / v_string.

ENDIF.

Thanks,

Naren

18 REPLIES 18
Read only

ferry_lianto
Active Contributor
0 Likes
1,864

Hi,

Please use TRANSLATE statement.

Regards,

Ferry Lianto

Read only

Former Member
0 Likes
1,864

Hi ramana,

Do you have the

REPLACE all occurances of '&' in v_str into v_target.

Regards

Ravi

Read only

0 Likes
1,864

I get error ...<b>unable to interpret occurances</b>

And can you give me the syntax..for translate in my case

Read only

0 Likes
1,864

it was a spelling mistake..

its occurences and not occurances.

Regards,

Ravi

Read only

0 Likes
1,864

I tried all these spellings in 4.6c

OCCURRENCES

OCCURENCES

OCCURANCES

all show an error

Read only

0 Likes
1,864

that option for REPLACE probably doesn't exist in 46c.. I use it on my 47..

~Suresh

Read only

ferry_lianto
Active Contributor
0 Likes
1,864

Hi,

Let's say you want to replace '&' with '1'.


loop at itab.
  tmp = itab-f1.

  TRANSLATE tmp using '&1'.

  itab-f1 = tmp.
endloop.

Regards,

Ferry Lianto

Read only

0 Likes
1,864

TRANSLATE tmp using '&1'.

this also didnot work for me.

I guess this is because ...I have given it like this..

translate lf_tmp using '&&amp;'.

I am translating & with '&amp;'

Message was edited by:

ramana peddu

Read only

Former Member
0 Likes
1,864

Hi Ramana,

Try using the following code.

LOOP AT itab.

  REPLACE ALL OCCURRENCES OF '&' IN itab-f1 WITH '<text string>'.

  MODIFY itab TRANSPORTING f1.

ENDLOOP.

where <text string> = String that should replace &

Let me know if this helps.

Read only

Former Member
0 Likes
1,864

Hi,

replace all occurences addition is not available in 4.6c..

You have to use TRANSLATE as mentioned by Ferry.

Thanks,

Naren

Read only

Former Member
0 Likes
1,864

Hi,

It is working...

Check this sample code with your string..

DATA: v_string TYPE string.

v_string = 'HOPT & SCHULER GMBH & CO KG'.

TRANSLATE v_string USING '&1'.

Thanks,

Naren

WRITE: / v_string.

Read only

Former Member
0 Likes
1,864

Hi,

Do you want to replace & with '&'

Before replacing

HOPT & SCHULER GMBH & CO KG

After replacing

HOPT '&' SCHULER GMBH '&' CO KG

Do you want this..

Thanks,

Naren

Read only

Former Member
0 Likes
1,865

Hi,

Okay..You can use the Function module WRB_UTIL_STRING_REPLACE

Try this code

DATA: v_string TYPE string.

v_string = 'HOPT & SCHULER GMBH & CO KG'.

CALL FUNCTION 'WRB_UTIL_STRING_REPLACE'

EXPORTING

pi_search_string = '&'

pi_replace_string = '''&'''

CHANGING

pc_string = v_string

EXCEPTIONS

search_string_too_long = 1

missing_search_string = 2

OTHERS = 3.

IF sy-subrc = 0.

WRITE: / v_string.

ENDIF.

Thanks,

Naren

Read only

0 Likes
1,864

I did it like this ...

CALL FUNCTION 'WRB_UTIL_STRING_REPLACE'

EXPORTING

pi_search_string = '&'

pi_replace_string = 'amp;'

changing

pc_string = lf_tmp

EXCEPTIONS

SEARCH_STRING_TOO_LONG = 1

MISSING_SEARCH_STRING = 2

OTHERS = 3

.

IF sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

It went into a dump.

<b>Thank you Naren I got it</b>

Message was edited by:

ramana peddu

Read only

Former Member
0 Likes
1,864

Hi,

The data type should be string..For all the parameters..ALso you should give '''&''' in the replace string parameter...Instead of '&'

Try this..



DATA: V_STRING TYPE STRING.
V_STRING = IF_TMP.
CALL FUNCTION 'WRB_UTIL_STRING_REPLACE'
EXPORTING
pi_search_string = '&'
pi_replace_string = '''&'''
changing
pc_string = v_string
EXCEPTIONS
SEARCH_STRING_TOO_LONG = 1
MISSING_SEARCH_STRING = 2
OTHERS = 3.

if_tmp = string.

Thanks,

Naren

Read only

Former Member
0 Likes
1,864

Hi,

Please make sure to reward points for helpful answers...

Thanks,

Naren

Read only

0 Likes
1,864

Sorry Naren , I forgot to reward points ...see I have done it

Read only

ferry_lianto
Active Contributor
0 Likes
1,864

Hi,

Please try this.


data: v_str1(1) type c value '&',
      v_str2(3) type c value '''&''',
      v_len type i.

loop at itab.
   lf_tmp = itab-f1.

   if lf_tmp cs v_str1.
     v_len = strlen( lf_tmp ).

     do v_len times. 
       replace v_str1 with v_str2 into lf_tmp.
     enddo   

   endif.

   itab-f1 = lf_tmp.
  modify itab.
endloop.

Regards,

Ferry Lianto