[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Remove only TRAILING whitespace

Bob Smyph

10/14/2008 6:47:00 PM

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-....

4 Answers

The Higgs bozo

10/14/2008 6:49:00 PM

0

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-....

Lyle Johnson

10/14/2008 6:50:00 PM

0

On Tue, Oct 14, 2008 at 1:46 PM, Bob Smyph
<eric.ramsey@cbc-companies.com> 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. ;)

Stephen Ball

10/14/2008 6:55:00 PM

0

str = " string "

rightTrim = str.rstrip
=> " string"

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

On Tue, 14 Oct 2008 14:46:43 -0400, Bob Smyph
<eric.ramsey@cbc-companies.com> 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



Bob Smyph

10/14/2008 6:57:00 PM

0

Thank you all for your fast help.
--
Posted via http://www.ruby-....