パンくず
Ruby Cookbook
空白詰め、空白除去
概要
空白詰め、空白除去
空白詰め
両端空白詰め(文字数指定)
str.center(num)
右空白詰め(文字数指定)
str.ljust(num)
左空白詰め(文字数指定)
str.rjust(num)
空白除去
両端空白除去
str.strip
右空白除去
str.lstrip
左空白除去
str.rstrip
サンプル
hoge = 'hoge' puts "[#{hoge.center(10)}]" # => 両端空白詰め(各5文字) puts "[#{hoge.ljust(10)}]" # => 右空白詰め(10文字) puts "[#{hoge.rjust(10)}]" # => 左空白詰め(10文字) space_hoge = ' hoge ' puts "[#{space_hoge.strip}]" # => 両端空白除去 puts "[#{space_hoge.lstrip}]" # => 右空白除去 puts "[#{space_hoge.rstrip}]" # => 左空白除去
出力
[ hoge ] [hoge ] [ hoge] [hoge] [hoge ] [ hoge]