[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Tomas Fischer

7/26/2006 11:41:00 AM

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


--
Posted via http://www.ruby-....

4 Answers

Vincent Fourmond

7/26/2006 11:48:00 AM

0


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


Matthew Harris

7/26/2006 12:27:00 PM

0

Alternatively you can reverse, sub, reverse, but I like Vince's way.

On 7/26/06, Vincent Fourmond <vincent.fourmond@9online.fr> wrote:
>
> 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
>
>
>


--
Matt

James Gray

7/26/2006 12:56:00 PM

0

On Jul 26, 2006, at 6:47 AM, Vincent Fourmond wrote:

>
> 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 ?

If that's for a single line string, drop the g and just use sub().

James Edward Gray II

Tomas Fischer

7/26/2006 1:46:00 PM

0

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

--
Posted via http://www.ruby-....