[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

string replacement...

Josselin

4/27/2007 7:29:00 AM

I have a string : str = "/proposal/list/31551"

I would like to change the 31551 by another value "9999", but I don't
see whicj method I should use ?

str.each and block ? or str.rindex('/') and concatenating the new value ?

tfyh

joss

15 Answers

Stefano Crocco

4/27/2007 7:37:00 AM

0

Alle venerdì 27 aprile 2007, Josselin ha scritto:
> I have a string : str = "/proposal/list/31551"
>
> I would like to change the 31551 by another value "9999", but I don't
> see whicj method I should use ?
>
> str.each and block ? or str.rindex('/') and concatenating the new value ?
>
> tfyh
>
> joss

If the text you need to replace is always at the end of the string, you can
do:

str.sub /\d+$/, '9999'

This will replace the text matching the regexp (i.e, one or more digit
followed by the end of the line) with the given string (note that this won't
change the original string, but return a new one. If you need to change the
original one, use sub! instead of sub).

I hope this helps

Stefano

Björn Paetzel

4/27/2007 7:37:00 AM

0

Josselin schrieb:
> I have a string : str = "/proposal/list/31551"
>
> I would like to change the 31551 by another value "9999", but I don't
> see whicj method I should use ?

How about:

str.gsub! "31551", "9999"

or to replace "the number at the end":

str.gsub! /[0-9]+$/, "9999"

:)

Dan Zwell

4/27/2007 7:39:00 AM

0

Josselin wrote:
> I have a string : str = "/proposal/list/31551"
>
> I would like to change the 31551 by another value "9999", but I don't
> see whicj method I should use ?
>
> str.each and block ? or str.rindex('/') and concatenating the new value ?
>
> tfyh
>
> joss
>
>

str.gsub!(/31551/, "9999")

good luck,
dan


William James

4/27/2007 7:52:00 AM

0

On Apr 27, 2:29 am, Josselin <josse...@wanadoo.fr> wrote:
> I have a string : str = "/proposal/list/31551"
>
> I would like to change the 31551 by another value "9999", but I don't
> see whicj method I should use ?
>
> str.each and block ? or str.rindex('/') and concatenating the new value ?
>
> tfyh
>
> joss

str[/\d+/] = "9999"

Alex Young

4/27/2007 8:01:00 AM

0

William James wrote:
> On Apr 27, 2:29 am, Josselin <josse...@wanadoo.fr> wrote:
>> I have a string : str = "/proposal/list/31551"
>>
>> I would like to change the 31551 by another value "9999", but I don't
>> see whicj method I should use ?
>>
>> str.each and block ? or str.rindex('/') and concatenating the new value ?
>>
>> tfyh
>>
>> joss
>
> str[/\d+/] = "9999"
Whoa. I hadn't seen that syntax before. Nice.

--
Alex

Robert Klemme

4/27/2007 9:15:00 AM

0

On 27.04.2007 09:29, Josselin wrote:
> I have a string : str = "/proposal/list/31551"
>
> I would like to change the 31551 by another value "9999", but I don't
> see whicj method I should use ?
>
> str.each and block ? or str.rindex('/') and concatenating the new value ?

How about

irb(main):006:0> str = "/proposal/list/31551"
=> "/proposal/list/31551"
irb(main):007:0> str[%r{\d+$}]="9999"
=> "9999"
irb(main):008:0> str
=> "/proposal/list/9999"

If it's a file name you could also do

irb(main):012:0> File.join(File.dirname(str), "9999")
=> "/proposal/list/9999"

I estimate there are another 5 million other ways around. :-)

Kind regards

robert

Josselin

4/27/2007 1:11:00 PM

0

On 2007-04-27 09:36:31 +0200, Stefano Crocco <stefano.crocco@alice.it> said:

> Alle venerdì 27 aprile 2007, Josselin ha scritto:
>> I have a string : str = "/proposal/list/31551"
>>
>> I would like to change the 31551 by another value "9999", but I don't
>> see whicj method I should use ?
>>
>> str.each and block ? or str.rindex('/') and concatenating the new value ?
>>
>> tfyh
>>
>> joss
>
> If the text you need to replace is always at the end of the string, you can
>
> do:
>
> str.sub /\d+$/, '9999'
>
> This will replace the text matching the regexp (i.e, one or more digit
> followed by the end of the line) with the given string (note that this won'
> t
> change the original string, but return a new one. If you need to change the
>
> original one, use sub! instead of sub).
>
> I hope this helps
>
> Stefano

Thnaks Stefano , it gives the expected result

irb(main):007:0> str = "/proposals/list/31555"
=> "/proposals/list/31555"
irb(main):008:0> str.sub /\d+$/, '9999'
=> "/proposals/list/9999"

;-)))

Josselin

4/27/2007 1:14:00 PM

0

On 2007-04-27 09:37:25 +0200, Björn Paetzel <news@kolrabi.de> said:

> Josselin schrieb:
>> I have a string : str = "/proposal/list/31551"
>>
>> I would like to change the 31551 by another value "9999", but I don't
>> see whicj method I should use ?
>
> How about:
>
> str.gsub! "31551", "9999"
>
> or to replace "the number at the end":
>
> str.gsub! /[0-9]+$/, "9999"
>
> :)

thanks Björn , expected result for digits, but I forgot any other
characters , replacing the item after the last / was my goal

irb(main):007:0> str = "/proposals/list/31555"
=> "/proposals/list/31555"
irb(main):012:0> str.gsub! /[0-9]+$/, "9999"
=> "/proposals/list/9999"

but

irb(main):013:0> str2 = "/proposals/list/abcde"
=> "/proposals/list/abcde"
irb(main):014:0> str2.gsub! /[0-9]+$/, "9999"
=> nil


josss :-))

Josselin

4/27/2007 1:15:00 PM

0

On 2007-04-27 09:39:04 +0200, Dan Zwell <dzwell@gmail.com> said:

> Josselin wrote:
>> I have a string : str = "/proposal/list/31551"
>>
>> I would like to change the 31551 by another value "9999", but I don't
>> see whicj method I should use ?
>>
>> str.each and block ? or str.rindex('/') and concatenating the new value ?
>>
>> tfyh
>>
>> joss
>>
>>
>
> str.gsub!(/31551/, "9999")
>
> good luck,
> dan

thanks Dan, but I looked for more generic replacement.. as Stefano mentionned

joss

Josselin

4/27/2007 1:17:00 PM

0

On 2007-04-27 09:52:29 +0200, William James <w_a_x_man@yahoo.com> said:

> On Apr 27, 2:29 am, Josselin <josse...@wanadoo.fr> wrote:
>> I have a string : str = "/proposal/list/31551"
>>
>> I would like to change the 31551 by another value "9999", but I don't
>> see whicj method I should use ?
>>
>> str.each and block ? or str.rindex('/') and concatenating the new value ?
>>
>> tfyh
>>
>> joss
>
> str[/\d+/] = "9999"

irb(main):007:0> str = "/proposals/list/31555"
=> "/proposals/list/31555"
irb(main):008:0> str.sub /\d+$/, '9999'
=> "/proposals/list/9999"
irb(main):009:0> str[/\d+/] = "9999"
=> "9999"

got it from Stefano... Thanks Bill