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
3,681

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

1 ACCEPTED SOLUTION
Read only

JozsefSzikszai
Active Contributor
0 Likes
2,273

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

11 REPLIES 11
Read only

Former Member
0 Likes
2,273

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^

Read only

Former Member
0 Likes
2,273

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

Read only

JozsefSzikszai
Active Contributor
0 Likes
2,274

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

Read only

Former Member
0 Likes
2,273

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.

Read only

0 Likes
2,273

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.

Read only

dmitry_sharshatkin
Product and Topic Expert
Product and Topic Expert
2,273

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

Read only

0 Likes
2,273

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

Read only

0 Likes
2,273

Hi Rich,

Does not work with commas...

BR, Dima

Read only

0 Likes
2,273

Did you modify the regex - I did say x.  The OP did not specify another character.

Rich

Read only

0 Likes
2,273

For commas:

,(?=[^,]*$)

Read only

0 Likes
2,273

Thanks! Works nice, will learn regex.

BR, Dima