Remove only TRAILING whitespace

Discussion in 'Ruby' started by Bob Smyph, Oct 14, 2008.

  1. Bob Smyph

    Bob Smyph Guest

    Is there any way to only remove trailing white space from a string. I
    have tried to use strip but that will also take off leading white space
    and I want to keep that.

    any help is appreciated
    --
    Posted via http://www.ruby-forum.com/.
     
    Bob Smyph, Oct 14, 2008
    #1
    1. Advertising

  2. Bob Smyph wrote:
    > Is there any way to only remove trailing white space from a string. I
    > have tried to use strip but that will also take off leading white space
    > and I want to keep that.
    >
    > any help is appreciated


    irb(main):001:0> "sldkfj ".sub(/\s+\Z/, "")
    => "sldkfj"
    --
    Posted via http://www.ruby-forum.com/.
     
    The Higgs bozo, Oct 14, 2008
    #2
    1. Advertising

  3. Bob Smyph

    Lyle Johnson Guest

    On Tue, Oct 14, 2008 at 1:46 PM, Bob Smyph
    <> wrote:

    > Is there any way to only remove trailing white space from a string. I
    > have tried to use strip but that will also take off leading white space
    > and I want to keep that.


    String#rstrip will remove just the white space on the end (the "right"
    side) of the string. Likewise, String#lstrip removes leading white
    space only. And you already know about String#strip. ;)
     
    Lyle Johnson, Oct 14, 2008
    #3
  4. Bob Smyph

    Stephen Ball Guest

    str = " string "

    rightTrim = str.rstrip
    => " string"

    # for completeness
    leftTrim = str.lstrip
    => "string "

    On Tue, 14 Oct 2008 14:46:43 -0400, Bob Smyph
    <> wrote:

    > Is there any way to only remove trailing white space from a string. I
    > have tried to use strip but that will also take off leading white space
    > and I want to keep that.
    >
    > any help is appreciated
     
    Stephen Ball, Oct 14, 2008
    #4
  5. Bob Smyph

    Bob Smyph Guest

    Bob Smyph, Oct 14, 2008
    #5
    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. Brad Marts
    Replies:
    3
    Views:
    412
    Brad Marts
    Nov 17, 2003
  2. Donald Canton

    remove trailing whitespace from string

    Donald Canton, Feb 9, 2004, in forum: C++
    Replies:
    5
    Views:
    11,207
    Tilman Kuepper
    Feb 9, 2004
  3. KevinGPO
    Replies:
    1
    Views:
    342
    Richard Herring
    Feb 20, 2006
  4. Dotan Cohen
    Replies:
    2
    Views:
    514
    Dotan Cohen
    Oct 10, 2011
  5. Taylor Strait
    Replies:
    13
    Views:
    382
    Chris Gernon
    Dec 28, 2006
Loading...

Share This Page