Remove leading and trailing spaces

Try strip or strip!

Good luck,

-Conrad

What would be the senario that you'd use strip! please?

Depends what effect you want to see: the ! version operates on the
String object itself, the other returns a new String as the result of
the strip operation:

s = ' xyz '

s2 = s.strip
s => ' xyz ' # s is unchanged
s2 => 'xyz'

s.strip!
s => 'xyz' # s changes

strip returns its result, strip! modifies it’s caller directly

Grace Xue wrote:

Grace Xue wrote:

Mike wrote:

s = ' xyz '
    
s2 = s.strip
s => ' xyz ' # s is unchanged
s2 => 'xyz'

s.strip!
s => 'xyz' # s changes
    
If my s = 'xyz' in the first place, would s.srip! cause any problem, as the documentation seems to suggest that s.strip! in this case will return NIL?
  
Hi Grace,

You are right!

s.strip! will *return* nil. So, if you do
t = s.strip! then t will be nil as you pointed out. s will still be valid.

Cheers,
Mohit.
8/22/2007 | 11:42 AM.