Strip with some other character

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.

On Thu, Aug 7, 2008 at 5:03 PM, Junkone [email protected] 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

2008/8/8 Martin DeMello [email protected]:

On Thu, Aug 7, 2008 at 5:03 PM, Junkone [email protected] 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

On Aug 8, 5:04 am, Junkone [email protected] 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) ‘,’

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs