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: 

Replace

Former Member
0 Kudos

Hi Gurus,

I have a problem with the REPLACE command.

lets say I need to replace 'ITAB' with 'I_ITAB'.

But actually its also replacing in 'ITAB1' also coz it contains 'ITAB' in it.

I tried using LENGTH option in the REPLACE command .But it didnt work out.

Please help .

Regards,

V S L Bharathi K

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Please go through this code. This will solve your issue.

DATA: fir TYPE i,

sec TYPE i,

result_tab TYPE match_result_tab,

res TYPE LINE OF match_result_tab,

BEGIN OF tab OCCURS 0,

line(255),

END OF tab.

tab-line = 'this is ITAB line'.

APPEND tab.

CLEAR tab-line.

tab-line = 'this is ITAB1 line'.

APPEND tab.

CLEAR tab-line.

tab-line = 'this is ITAB line'.

APPEND tab.

CLEAR tab-line.

tab-line = 'this is ITAB1 line'.

APPEND tab.

CLEAR tab-line.

LOOP AT tab.

FIND ALL OCCURRENCES OF 'ITAB' IN

tab-line

RESULTS result_tab.

LOOP AT result_tab INTO res.

fir = res-offset - 1.

sec = res-offset + res-length.

IF tab-linefir(1) = space AND tab-linesec(1) = space.

REPLACE ALL OCCURRENCES OF 'ITAB' IN tab-line WITH 'I_ITAB'

REPLACEMENT OFFSET res-offset REPLACEMENT LENGTH res-length.

ENDIF.

ENDLOOP.

MODIFY tab TRANSPORTING line.

ENDLOOP.

LOOP AT tab.

WRITE : / tab-line.

ENDLOOP.

Hope this will help you.

Regards,

Smart Varghese

9 REPLIES 9

Former Member
0 Kudos

Hi ,

You need to use REPLACE thrice .

First you need to replace ITAB1 with the most unused string (in the below EX: 'XYZ').

Now replace ITAB to I_ITAB.

Now repace back 'XYZ' to ITAB1.

Check bellow code - -

DATA : w_string TYPE string VALUE 'ITAB ITAB1'.
REPLACE 'ITAB1' IN w_string WITH 'XYZ'. " replacing ITAB1 to XYZ
REPLACE 'ITAB' IN w_string WITH 'I_ITAB'. " Replacing ITAB to I_ITAB
REPLACE 'XYZ' IN w_string WITH 'ITAB1'.  " Replacing back XYZ to ITAB1
WRITE : w_string.

If you want replace all the occurrences try this - -

DATA : w_string TYPE string VALUE 'ITAB ITAB1'.
REPLACE ALL OCCURRENCES OF 'ITAB1' IN w_string WITH 'XYZ'.
.
.

Regards

Pinaki

rainer_hbenthal
Active Contributor
0 Kudos

are regular expressions available on your system?

Former Member
0 Kudos

Hi,

If for eg variable contains 'ITAB ITAB1'.

Replace first occurence of 'ITAB' in <variable name> with <new value>.

U can also use offset value and length to replace a specific area in a string.

Replace 'ITAB' in SECTION [OFFSET off] [LENGTH len]

with <new value>.

specify offset anf lenght which defines the section for which the replacement ll take place.

regards,

ajit.

Edited by: AJIT THAKUR on Jun 4, 2009 8:05 AM

0 Kudos

Hi Ajit ,

I problem is i need replace a string in the whole program automaticaly using another program .

So I will not be knowing the position of the string where it is present to use the offset value .

Pleas help.

Regards,

V S L Bharathi K

0 Kudos

Hi Kamala ,

As because you will be replacing some contents of a program , from a program ,

inside the loop you need to replace from the field string as I told last post.

LOOP AT internal_table TO field_string.
REPLACE ALL OCCURRENCES OF 'ITAB1' IN fieldng WITH 'XYZ100'. " replacing ITAB1 to XYZ100
REPLACE ALL OCCURRENCES OF 'ITAB' IN fieldng WITH 'I_ITAB'. " Replacing ITAB to I_ITAB
REPLACE ALL OCCURRENCES OF 'XYZ100' IN fieldng WITH 'ITAB1'.  " Replace back XYZ100 to ITAB1
ENDLOOP.

Definitely it will resolve the issue.

Regards

Pinaki

Former Member
0 Kudos

example:

LOOP AT it_otfdata1 INTO wotfdata .

REPLACE ALL OCCURRENCE OF 'DUPLICATE' IN wotfdata-tdprintpar WITH 'ORIGINAL'.

IF sy-subrc = 0.

MODIFY it_otfdata1 FROM wotfdata INDEX sy-tabix TRANSPORTING tdprintpar.

ENDIF.

ENDLOOP.

Former Member
0 Kudos

Hi,

Please go through this code. This will solve your issue.

DATA: fir TYPE i,

sec TYPE i,

result_tab TYPE match_result_tab,

res TYPE LINE OF match_result_tab,

BEGIN OF tab OCCURS 0,

line(255),

END OF tab.

tab-line = 'this is ITAB line'.

APPEND tab.

CLEAR tab-line.

tab-line = 'this is ITAB1 line'.

APPEND tab.

CLEAR tab-line.

tab-line = 'this is ITAB line'.

APPEND tab.

CLEAR tab-line.

tab-line = 'this is ITAB1 line'.

APPEND tab.

CLEAR tab-line.

LOOP AT tab.

FIND ALL OCCURRENCES OF 'ITAB' IN

tab-line

RESULTS result_tab.

LOOP AT result_tab INTO res.

fir = res-offset - 1.

sec = res-offset + res-length.

IF tab-linefir(1) = space AND tab-linesec(1) = space.

REPLACE ALL OCCURRENCES OF 'ITAB' IN tab-line WITH 'I_ITAB'

REPLACEMENT OFFSET res-offset REPLACEMENT LENGTH res-length.

ENDIF.

ENDLOOP.

MODIFY tab TRANSPORTING line.

ENDLOOP.

LOOP AT tab.

WRITE : / tab-line.

ENDLOOP.

Hope this will help you.

Regards,

Smart Varghese

0 Kudos

Hi Varghese,

Thank you for your reply ... it gave me the solution .

i have one more issue ... in the solution u gave ...i need to use 2 loops ..ie., loop in a loop...

can i avoid that and use something else....to avoid performance issues ??

Please help,

V S L Bharathi K

Former Member
0 Kudos

Hi Bharathi,

Why we need to use Loop inside a loop because we are not sure how many occurences will be there

for the searching string in a line.

If you are sure that only one occurance will be there, then instead of looping through RESULT_TAB we can have a read on RESULT_TAB with INDEX 1.

Mean while let me check any other possibilities.

Regards,

Smart Varghese