このマニュアルは既にメンテナンスが終了したバージョンの Ruby を対象としています。 最新版のマニュアルへ

Ruby 3.0 リファレンスマニュアル

instance method String#strip

strip -> String[permalink][rdoc][edit]

文字列先頭と末尾の空白文字を全て取り除いた文字列を生成して返します。空白文字の定義は " \t\r\n\f\v\0" です。

p "  abc  \r\n".strip    # => "abc"
p "abc\n".strip          # => "abc"
p "  abc".strip          # => "abc"
p "abc".strip            # => "abc"
p "  \0  abc  \0".strip  # => "abc"

str = "\tabc\n"
p str.strip              # => "abc"
p str                    # => "\tabc\n" (元の文字列は変化しない)

[SEE_ALSO] String#lstrip, String#rstrip