October 2006
Intermediate to advanced
888 pages
16h 55m
English
Ruby’s String class offers a rich set of methods for controlling case. This section offers an overview of these.
The downcase method converts a string to all lowercase. Likewise upcase converts it to all uppercase:
s1 = "Boston Tea Party" s2 = s1.downcase # "boston tea party" s3 = s2.upcase # "BOSTON TEA PARTY"
The capitalize method capitalizes the first character of a string while forcing all the remaining characters to lowercase:
s4 = s1.capitalize # "Boston tea party" s5 = s2.capitalize # "Boston tea party" s6 = s3.capitalize # "Boston tea party"
The swapcase method exchanges the case of each letter in a string:
s7 = "THIS IS AN ex-parrot." s8 = s7.swapcase # "this is an EX-PARROT."
As of Ruby 1.8, ...
Read now
Unlock full access