0
0

More than 1 year has passed since last update.

【Ruby】trimしたいときはstripしよう

Posted at

strip

Rubyにはtrimメソッドがないので代わりにstripメソッドを使う。

文字列先頭末尾の空白文字を全て取り除いた文字列を生成して返す。
空白文字の定義は" \t\r\n\f\v"
文字列右側からは"\0"も取り除くが、左側の"\0"は取り除かない。

pry(main)> p "   s".strip
=> "s"
# 文字と文字の間の空文字は取り除かない
pry(main)> p "   s  f  ".strip
=> "s  f"
pry(main)> p "   s  f\n".strip
=> "s  f"
pry(main)> p "\0  s  f\0".strip
=> "\u0000  s  f"

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0