issue with stripping tabs

H

helmut_blass

without success I try to strip leading and trailing tabs:
I tried:
str.gsub(/\s+/, "")
str.gsub(/\t/, "")
str.strip.gsub(/\t/, "")

none of them works.

any suggestions?
thanx, Helmut
 
R

Robert Klemme

without success I try to strip leading and trailing tabs:
I tried:
str.gsub(/\s+/, "")
str.gsub(/\t/, "")
str.strip.gsub(/\t/, "")

none of them works.

any suggestions?

Where's your problem? They all work - in a way

irb(main):004:0> strings=["a", "\ta", "a\t", "\ta\tb\t"]
=> ["a", "\ta", "a\t", "\ta\tb\t"]
irb(main):005:0> strings.map {|str| str.gsub(/\s+/, "")}
=> ["a", "a", "a", "ab"]
irb(main):006:0> strings.map {|str| str.gsub(/\t/, "")}
=> ["a", "a", "a", "ab"]
irb(main):007:0> strings.map {|str| str.strip.gsub(/\t/, "")}
=> ["a", "a", "a", "ab"]

You might want to do

irb(main):008:0> strings.map {|str| str.sub(/\A\t+/, "").sub(/\t+\z/, "")}
=> ["a", "a", "a", "a\tb"]

But #strip is much simpler, even though it removes other whitespace as well:

irb(main):009:0> strings.map {|str| str.strip}
=> ["a", "a", "a", "a\tb"]

Cheers

robert
 

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
474,039
Messages
2,570,376
Members
47,031
Latest member
AndreBucki

Latest Threads

Top