issue with stripping tabs

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

  1. helmut_blass

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

  2. 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
     
    Robert Klemme, Nov 23, 2013
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.