str = " hi \t"
p str.strip
p str.rstrip
p str.lstrip
"hi" " hi" "hi \t"
"hi \t"
"hi \n\n".chomp
"hi \n"
'Users'.chop
"User"
"foo.".sub(/\.$/,'')
"foo"
"foo."[0...-1]
"foo"
'yahoooooooooooooooooo'.squeeze
"yaho"
'aabbbccdd'.squeeze('abc')
"abcdd"
'aabbbccdd'.squeeze('a-c')
"abcdd"
'aabbbccdd'.squeeze('^a')
"aabcd"
' aabb'.squeeze("\s")
" aabb"
' aabb'.squeeze('\s')
" aabb"
"\s"
" "
p 'ABC'.downcase
p 'abc'.upcase
p 'Abc'.swapcase
p 'tiTle'.capitalize
"abc" "ABC" "aBC" "Title"
"Title"
'55-9-7-24'.sub(/[0-9]+/, 'x')
"x-9-7-24"
'55-9-7-24'.gsub(/[0-9]+/, 'x')
"x-x-x-x"
'55-9-7-24'.gsub(/[0-9]+/) {|str| str.to_i.succ}
"56-10-8-25"
1.succ
2
1.pred
0
'55-9-7-24'.gsub(/([0-9]+)/) { $1.to_i.succ }
"56-10-8-25"
str = ' hi '
p str.strip!
p str.strip!
"hi" nil
p 'dam'.reverse
p 'ダムダムダムダムダム'.reverse
"mad" "ムダムダムダムダムダ"
"ムダムダムダムダムダ"
str = 'dam' str.reverse! str
str = 'America, Briten, Canada'
p str.split(',')
p str.split(/,\s+/)
["America", " Briten", " Canada"] ["America", "Briten", "Canada"]
["America", "Briten", "Canada"]
str.split(/,\s+/, 2)[1]
"Briten, Canada"
'Alice'.split(//)
'Alice'.each_char.to_a
["A", "l", "i", "c", "e"]
'Alice'.each_byte.to_a
'ブフー'.each_char{|c| p c}
"ブ" "フ" "ー"
"ブフー"
"Alice\nBob\nCarrot".each_line{|line| p line}
"Alice\n" "Bob\n" "Carrot"
"Alice\nBob\nCarrot"
$/
"\n"
"Alice\tBob\tCarrot".each_line("\t"){|line| p line}
"Alice\t" "Bob\t" "Carrot"
"Alice\tBob\tCarrot"
次回は「5-2-6 エンコーディングの扱い」から