How to convert a string to UPPERCASE or lowercase in Ruby?

来源:转载

Ruby strings have methods to convert them to uppercase and lowercase. The method names of the methods are upcaseand downcaserespectively.

Calling the downcaseor upcasemethod on a string will return the lowercase or uppercase version of the string, but the original variable won't change.

name = 'Foo'puts name # Rubyputs name.downcase # rubyputs name.upcase # RUBYputs name # Ruby

Calling the same methods followed by an exclamation mark will both return lowercase/uppercase version of the string and will also change the content of the variable.

puts name.downcase! # rubyputs name # rubyputs name.upcase! # RUBYputs name # RUBY Full example

examples/ruby/case.rb

name = 'Foo'puts name # Rubyputs name.downcase # rubyputs name.upcase # RUBYputs name # Rubyputs name.downcase! # rubyputs name # rubyputs name.upcase! # RUBYputs name # RUBY


分享给朋友:
您可能感兴趣的文章:
随机阅读: