Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > issue with stripping tabs

Reply
Thread Tools

issue with stripping tabs

 
 
helmut_blass@web.de
Guest
Posts: n/a
 
      11-23-2013
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
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      11-23-2013
On 23.11.2013 10:07, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unicode issue with Python v3.3 Νίκος Γκρ33κ Python 80 04-19-2013 07:16 PM
Help with printing a bit pattern with printf and %x matt.jaffe@gmail.com C Programming 9 04-19-2013 12:14 PM
Tabs -vs- Spaces: Tabs should have won. rantingrick Python 95 07-18-2011 11:07 PM
CSV::Writer... Using tabs instead of commas (or creating excel file using tabs to seperate data) John Kopanas Ruby 2 01-29-2007 06:26 PM
List text files showing LFs and expanded tabs (was: Colorize expanded tabs) qwweeeit Python 2 12-14-2005 10:07 AM



Advertisments