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 the last character

Former Member
0 Likes
9,756

I have a requirement.....I have strinf for ex. INT,CHAR,BHB,BNVB,

I have to replace the last occurance of , into .

It will look like  INT,CHAR,BHB,BNVB.

Please help.

1 ACCEPTED SOLUTION
Read only

UweFetzer_se38
Active Contributor
0 Likes
5,325

Why so complicated if you can make it simple?

REPLACE REGEX ',$' in gv_string WITH '.'.

Best regards

8 REPLIES 8
Read only

Former Member
0 Likes
5,325

Hi Sanghamitra,

You can do 2 things.

1. Reverse the string and use REPLACE FIRST OCCURRENCE statement.

2. Or else, search for the offset of last , and then replace that offset using REPLACE SECTION statement.

Hope it helps you.

Regards,

Sindhu Pulluru.

Read only

Former Member
0 Likes
5,325

Hi,

You can try the following:

data: v_str type string,

        l_lines type i.

v_str = ' INT,CHAR,BHB,BNVB,'.

Split v_str at ',' into table itab.

describe table itab lines l_lines.

read table itab into w_tab index l_lines.

if sy-subrc eq 0.

replace ',' with '.' in w_tab.

modify itab from w_tab index l_lines.

endif.

Read only

UweFetzer_se38
Active Contributor
0 Likes
5,326

Why so complicated if you can make it simple?

REPLACE REGEX ',$' in gv_string WITH '.'.

Best regards

Read only

0 Likes
5,325

Very nice hint! But I had to use '.$' instead of ',$' to make it work.

Read only

mayur_priyan
Active Participant
0 Likes
5,325

Hi,

You can try the following

   data: lv_data TYPE string value 'int,ban,sit,viu,',
      lv_len TYPE i,
      lv_data1 TYPE char25.

lv_data1 = lv_data.
lv_len = strlen( lv_data ).
lv_len = lv_len - 1.
lv_data1+lv_len(1) = '.'.
write: / lv_data.
        skip.
write: / lv_data1.

Read only

Former Member
0 Likes
5,325

Hello Sanghamitra,

When you say 'last occurrence...' does that mean the last character? If so, Uwe's statement (and Mayur's code) would work.

But if the below string qualifies for your idea of 'last occurrence' then the solutions given by Uwe and Mayur won't work.


INT,CHAR,BHB,BNVB


Can you clarify?


-Amit.

Read only

0 Likes
5,325

Note the date of the OP.

Rob

Read only

0 Likes
5,325

Oops...