gsub

T

Tomas Fischer

Hi,

I have a string s= XXaaXX, in which i want to replace the last substring
r, which is determined at run time.

r= XX

s.gsub(/XX\z/,'') solves the problem in a static way.
s.gsub(r,'') solves it too, but who can I specify, that only the last
occurence should be replaced.

Best regards,
Tomas
 
V

Vincent Fourmond

Hello !
s.gsub(/XX\z/,'') solves the problem in a static way.
s.gsub(r,'') solves it too, but who can I specify, that only the last
occurence should be replaced.

Would
s.gsub(/#{r}$/,'')
do what you want ?

Vince
 
T

Tomas Fischer

Hi,

thanks for your solutions. But I've forgotten to mention, that r can
contain brackets, so the simple way s.gsub(/#{r}$/,'') doesn't work.

I use now the reverse, sub approach.

Best regards,
Tomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,684
Latest member
sparada

Latest Threads

Top