Ruby/String/String format
Материал из Wiki.crossplatform.ru
Версия от 17:56, 13 сентября 2010; ViGOur (Обсуждение | вклад)
Содержание |
Center, left justify and right justify string
s = "Some text." s.center(15) # => " Some text. " s.ljust(15) # => "Some text. " s.rjust(15) # => " Some text."
Changing the Case of a String
s = "HELLO, I am not here. I WENT to tHe MaRKEt." s.upcase # => "HELLO, I AM NOT HERE. I WENT TO THE MARKET." s.downcase # => "hello, i am not here. i went to the market." s.swapcase # => "hello, i AM NOT HERE. i went TO ThE mArkeT." s.capitalize # => "Hello, i am not here. i went to the market."
Replace string with float
"To 2 decimal places: %.2f" % Math::PI # => "To 2 decimal places: 3.14" "Zero-padded: %.5d" % Math::PI # => "Zero-padded: 00003"
Substituting Variables Into an Existing String
template = "O with %s." puts template % "E"
use % for format printing
puts "%s, %s!" % [ "Hello", "Matz" ]