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

Alternative for REPLACE

Former Member
0 Likes
1,468

Hi

I am using the following syntax of REPLACE in a report.

REPLACE old_character IN my_object WITH new_character.

The unique thing about this report is that it has to run

on different versions from 46c.

The above syntax works ok in abap version 7 but is not downward compatible to 46c.

Can anybody suggest something (of similar functionality) which will work across releases.

Will the older syntax of replace do?

REPLACE old_character WITH new_character INTO my_object.

(Abap help says this is obsolete.)

Any possible help is very much appreciated.

Regards

Sahir

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,043

Hope this works...

DATA text(16) VALUE 'THIS IS OLD TEXT'.
DATA new_str(25).
DATA temp(25).
DATA old_char(16) VALUE 'OLD'.
DATA new_char(16) VALUE 'NEW'.
DATA off TYPE i VALUE 0.
DATA moff TYPE i.
DATA mlen TYPE i.
DATA len TYPE i.
DATA flag TYPE i VALUE 0.


len = strlen( new_char ).

DO.
flag = 0.

FIND old_char IN SECTION
  OFFSET off OF text
  MATCH OFFSET moff
  MATCH LENGTH mlen
  .

IF sy-subrc = 0.
  flag = 1.
  WRITE : moff, mlen.
ENDIF.

IF flag = 0.
  off = off + mlen.
  WRITE : / text+off.
  CONCATENATE new_str text+off INTO new_str SEPARATED BY SPACE.
  EXIT.

ENDIF.

*Change repl

CONCATENATE new_str text+off(moff) INTO new_str.
CONCATENATE new_str new_char INTO new_str SEPARATED BY SPACE.

off = moff + 1.

ENDDO.
WRITE / new_str.

Ps: Reward points if helpful.

7 REPLIES 7
Read only

Former Member
0 Likes
1,043

HI,

Build logic using search ,overlay,move.

Regards

Amole

Read only

Former Member
0 Likes
1,043

you need to use split and concatenate.

regards

vijay

Read only

Former Member
0 Likes
1,044

Hope this works...

DATA text(16) VALUE 'THIS IS OLD TEXT'.
DATA new_str(25).
DATA temp(25).
DATA old_char(16) VALUE 'OLD'.
DATA new_char(16) VALUE 'NEW'.
DATA off TYPE i VALUE 0.
DATA moff TYPE i.
DATA mlen TYPE i.
DATA len TYPE i.
DATA flag TYPE i VALUE 0.


len = strlen( new_char ).

DO.
flag = 0.

FIND old_char IN SECTION
  OFFSET off OF text
  MATCH OFFSET moff
  MATCH LENGTH mlen
  .

IF sy-subrc = 0.
  flag = 1.
  WRITE : moff, mlen.
ENDIF.

IF flag = 0.
  off = off + mlen.
  WRITE : / text+off.
  CONCATENATE new_str text+off INTO new_str SEPARATED BY SPACE.
  EXIT.

ENDIF.

*Change repl

CONCATENATE new_str text+off(moff) INTO new_str.
CONCATENATE new_str new_char INTO new_str SEPARATED BY SPACE.

off = moff + 1.

ENDDO.
WRITE / new_str.

Ps: Reward points if helpful.

Read only

abdul_hakim
Active Contributor
0 Likes
1,043

Hi

Use REPLACE A1 WITH A2 INTO A3 syntax variant..

Abdul

Read only

Former Member
0 Likes
1,043

Hi,

This one replaces all occurances of an old char with new char using no special commands .

data : my_obj(100) value 'This is Old Text', new_char value 'a' ,
old_char value 'i'.
data : x type i value 1, y type i, len type i.
describe field my_obj length len in byte mode.


while y ne len.
 if my_obj+y(x) eq old_char.
     my_obj+y(x)  = new_char.
 endif.
y = y + 1.
endwhile.

write : my_obj.

reward points if helpful

Read only

Former Member
0 Likes
1,043

Hi

I would like to clarify something.

I am trying to replace one string with another in a big string. (Not characters, as i mentioned in the earlier message, sorry for that!)

Thanks for all the replies. I am trying out some of them.

Regards

Sahir

Read only

0 Likes
1,043

Hi Sahir,

the code given above is can be applied to string of any length and even string replacements can be done using the same operation.

this minor change in the previous code ca be made.

data : my_obj(100) value 'This is Old Text', new_char(100) value 'abcd' ,old_char(100) value 'is' ,str1(100),str2(100).

data : x type i value 1, y type i, len type i , idx type i , offset like sy-index.

describe field my_obj length len in byte mode.

while y ne len.

if my_obj+y(x) eq space.

while my_obj+y(x) ne space or y eq len.

flag = 1.

offset = sy-index.

if my_objy(x) eq old_charidx(x).

continue.

else.

flag = 0.

endif.

y = y + 1.

idx = sy-index.

endwhile.

y = sy-index.

endwhile.

if flag = 1.

str1 = my_obj+(offset).

str2 = my_obj+idx(*).

concatenate str1 new_char str2 into my_obj seperated by spaces.

write : my_obj.

please Award points if the answer is helpful