strip with some other character

J

Junkone

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.
 
M

Martin DeMello

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
 
R

Robert Klemme

2008/8/8 Martin DeMello said:
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
 
S

sathyz

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) ','
 

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
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top