[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

strip with some other character

Junkone

8/8/2008 12:04:00 AM

strip function removes the trailing whitespace. i want to remove
trailing comma's. is there a additional parameter for strip or some
other function to do the same.
3 Answers

Martin DeMello

8/8/2008 12:10:00 AM

0

On Thu, Aug 7, 2008 at 5:03 PM, Junkone <junkone1@gmail.com> wrote:
> strip function removes the trailing whitespace. i want to remove
> trailing comma's. is there a additional parameter for strip or some
> other function to do the same.

string.gsub(/^,+/, '').gsub(/,+$/, '')

martin

Robert Klemme

8/8/2008 1:21:00 PM

0

2008/8/8 Martin DeMello <martindemello@gmail.com>:
> On Thu, Aug 7, 2008 at 5:03 PM, Junkone <junkone1@gmail.com> wrote:
>> strip function removes the trailing whitespace. i want to remove
>> trailing comma's. is there a additional parameter for strip or some
>> other function to do the same.
>
> string.gsub(/^,+/, '').gsub(/,+$/, '')

This can be done with a single gsub:

irb(main):001:0> s=",,a,,"
=> ",,a,,"
irb(main):002:0> s.gsub %r{^,+|,+$}, ''
=> "a"

However, both strip trailing and leading commas. So for trailing
commas this should be sufficient

irb(main):003:0> s.gsub %r{,+$}, ''
=> ",,a"

OP: And of course, use gsub! for inplace modification if you want that.

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

sathyz

8/10/2008 10:18:00 AM

0

On Aug 8, 5:04 am, Junkone <junko...@gmail.com> wrote:
> strip function removes the trailing whitespace. i want to remove
> trailing comma's. is there a additional parameter for strip or some
> other function to do the same.

a = "Ruby,"
a.chomp(',')
# a.chomp!(',')
# but this will remove only the last (one) ','