‎2008 Jul 07 8:09 AM
Hi ,
How to replace last occurrence of a character in a string .
eg: aaaaxaaaxaaaxaaa
so i need last x should be replaced with y
output : aaaaxaaaxaaayaaa
‎2008 Jul 07 8:19 AM
hi
bad
I am not sure if there is a direct way, but you can try the following:
1. Reverse the string with FM STRING_REVERSE, so the last x will be the first
2. Now you can use RAPLACE ... FIRST OCCURANCE ...
3. Reverse the string again with FM STRING_REVERSE
hope this helps
ec
‎2008 Jul 07 8:12 AM
hi
good
step 1- store the value in a string
step 2- get the lenght of the string
step 3- get the position of the last y from the string
step 4- use replace statement to replace the x with y
Thanks
mrutyun^
‎2008 Jul 07 8:18 AM
Hi,
Try this coding using the Function Module.
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.
Hope this helps you.
Rewars points of helpfull.
Thanks & Regards,
Y.R.Prem Kumar
‎2008 Jul 07 8:19 AM
hi
bad
I am not sure if there is a direct way, but you can try the following:
1. Reverse the string with FM STRING_REVERSE, so the last x will be the first
2. Now you can use RAPLACE ... FIRST OCCURANCE ...
3. Reverse the string again with FM STRING_REVERSE
hope this helps
ec
‎2008 Jul 07 8:20 AM
Hi,
plz try this :
data: text type string.
text = 'aaaaxaaaxaaaxaaa'.
REPLACE SECTION OFFSET 12 OF text WITH 'yaaa'.
write: text.text = aaaaxaaaxaaayaaa.
hope this helps you.
plz reward if useful.
thanks,
dhanashri.
‎2008 Jul 07 9:01 AM
try this code
data: len(2) type n,
count(2) type n,
count1(2) type n.
parameter str(40) type c.
start-of-selection.
len = strlen( str ).
clear count.
do len times.
if str+count(1) = 'X'.
COUNT1 = SY-INDEX.
ENDIF.
COUNT = COUNT + 1.
ENDDO.
count1 = count1 - 1.
IF COUNT1 = 0.
str+0(1) = 'y'.
ELSE.
str+count1(1) = 'y'.
ENDIF.
write:/ str.
‎2016 Mar 11 9:44 AM
I’mnot happy with all above, so I’m posting my variant:
data: str type string value 'A,B,C,D,E,F,G,H,',
ls_result type match_result.
find all occurrences of ',' in str results ls_result.
if sy-subrc = 0.
if ls_result-offset > 0 and
ls_result-offset < strlen( str ).
replace section offset ls_result-offset length 1 of str with '' in character mode.
condense str no-gaps.
endif.
endif.
write: str.
Regards, Dima
‎2016 Mar 11 10:56 AM
In your replace command use a regular expression to find the last occurrence of the character you wish to change.
This: "x(?:.(?!x))+$" (without the quotes) will find the last occurrence of x in a string.
Rich
‎2016 Mar 11 12:00 PM
‎2016 Mar 11 12:40 PM
Did you modify the regex - I did say x. The OP did not specify another character.
Rich
‎2016 Mar 11 12:46 PM
‎2016 Mar 11 1:08 PM