一个rails的字符串处理

string="My first string"
string.length #=>15
string.length.next #=>16
string.slice(3,5) #=>first
string.upcase #=>MY FIRST STRING
string.empty? #false
string.include? 'My' #true

hash={"key1"=>"val1","key2"=>"val2"}
string=""
hash.each {|k,v| string << "#{k} is #{v}\n"}
puts string
#key1 is val1
#key2 is val2

s=".sdrawkcab si gnirts sihT"
s.reverse #=>"This string is "

s="Hello"
s.upcase #=>"HELLO"
s.downcase #=>"hello"
s.swapcase #=>"hELLO"
s.capitalize #=>"Hello"

带自动换行的文本
要将一个充满各种不同空白的字符串转换为带换行符并以适当间隔格式化的字符串,以便能够在窗口中显示文本或作为电子邮件发送。
def warp(s,width=78)
s.gsub( / ( . { 1, # { width} } ) ( \s+|\z ) / , "\\1\n")
end
puts warp("This is a test",10)
#This is a
#test