Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

String#strip!

str.strip! → self or nil

Removes leading and trailing whitespace from the receiver. Returns the altered receiver, or nil if there was no change.

Refer to String#strip for the definition of whitespace.

Example
"  hello  ".strip!  #=> "hello"
"hello".strip!      #=> nil
See also #strip #sub #start_with? #sub! also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0