snippetrubyCritical
How to convert a string to lower or upper case in Ruby
Viewed 0 times
howupperrubyconvertstringcaselower
Problem
How do I take a string and convert it to lower or upper case in Ruby?
Solution
Ruby has a few methods for changing the case of strings. To convert to lowercase, use
Similarly,
If you want to modify a string in place, you can add an exclamation point to any of those methods:
Refer to the documentation for String for more information.
downcase:"hello James!".downcase #=> "hello james!"Similarly,
upcase capitalizes every letter and capitalize capitalizes the first letter of the string but lowercases the rest:"hello James!".upcase #=> "HELLO JAMES!"
"hello James!".capitalize #=> "Hello james!"
"hello James!".titleize #=> "Hello James!" (Rails/ActiveSupport only)If you want to modify a string in place, you can add an exclamation point to any of those methods:
string = "hello James!"
string.downcase!
string #=> "hello james!"Refer to the documentation for String for more information.
Code Snippets
"hello James!".downcase #=> "hello james!""hello James!".upcase #=> "HELLO JAMES!"
"hello James!".capitalize #=> "Hello james!"
"hello James!".titleize #=> "Hello James!" (Rails/ActiveSupport only)string = "hello James!"
string.downcase!
string #=> "hello james!"Context
Stack Overflow Q#1020568, score: 1808
Revisions (0)
No revisions yet.