rubyco(るびこ)の日記 RSSフィード

2013-07-28

String#stripは文字列前後の空白を削除します

  • String#stripは文字列前後の空白を削除します。
"  Hello, world!  ".strip #=> "Hello, world!"
>> "   Hello, world!   ".strip
=> "Hello, world!"

>> "\t\t\tHello, world!\n\n\n".strip
=> "Hello, world!"

>> s = "   Hello, world!   "
=> "   Hello, world!   "

>> s
=> "   Hello, world!   "

>> s.strip
=> "Hello, world!"
>> s
=> "   Hello, world!   " # String#strip は非破壊的

>> s.strip!
=> "Hello, world!"
>> s
=> "Hello, world!" # String#strip! は破壊的