
String to Lowercase in Ruby
In this article, you will discover how to handle lowercase letters in a string in Ruby.
Lowercase the first letter
Unlike when we want to put a string to uppercase in a Ruby string, there is no function in Ruby allowing us to put only the first letter in lowercase. Therefore, we must use the downcase function for this (see below).
Lowercase entire string with downcase
The downcase function will help you to modify the string and put all the letters in lowercase:
puts "HELLO WORLD".downcase
// output: "hello world"
Whatever the starting state of the string, the ending state will always be the same:
puts "Hello World".downcase
// output: "hello world"
puts "Hello world".downcase
// output: "hello world"
This is how we can manage the first letter of the string.
If there is a number or a special character in the string, it gives:
puts "hello world! 2".downcase
// output: "hello world! 2"
puts "Hello World! 2".downcase
// output: "hello world! 2"
puts "HELLO WORLD! 2".downcase
// output: "hello world! 2"