issue with stripping tabs

Discussion in 'Ruby' started by helmut_blass@web.de, Nov 23, 2013.

  1. Guest

    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
    , Nov 23, 2013
    #1
    1. Advertising

  2. On 23.11.2013 10:07, 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/
    Robert Klemme, Nov 23, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qwweeeit
    Replies:
    2
    Views:
    643
    qwweeeit
    Dec 14, 2005
  2. Evans
    Replies:
    1
    Views:
    269
    Evans
    Jun 28, 2008
  3. rantingrick

    Tabs -vs- Spaces: Tabs should have won.

    rantingrick, Jul 16, 2011, in forum: Python
    Replies:
    95
    Views:
    1,828
    Roy Smith
    Jul 19, 2011
  4. John Kopanas
    Replies:
    2
    Views:
    276
    Gregory Brown
    Jan 29, 2007
  5. Jeannie
    Replies:
    3
    Views:
    93
    Jeannie
    Apr 15, 2004
Loading...

Share This Page