Open In App

How to Convert a String to Lower or Upper Case in Ruby?

Last Updated : 28 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to convert a string to lower or upper case in Ruby. We can convert a string to lower or upper case using built-in methods provided in the Ruby language.

Using Downcase

The downcase method is used to convert the string to lowercase.

Syntax:

string.downcase

In the below example, the string is converted to lowercase using the downcase method and store the result in the variable and at last output both the original and lowercase string.

Ruby
# Define a string
string = "Geeks for Geeks"

# Convert the string to lowercase using downcase method
lowercase_string = string.downcase

# Output the result
puts "Original String: #{string}"
puts "String in Lowercase: #{lowercase_string}"

Output
Original String: Geeks for Geeks
String in Lowercase: geeks for geeks

Using Upcase

The upcase method is used to convert the string to uppercase

Syntax:

string.upcase

In the below example,the string is converted to uppercase using the uocase method and store the result in the variable and at last output both the original and uppercase string.

Ruby
# Define a string
string = "Geeks For Geeks"

# Convert the string to uppercase using upcase method
uppercase_string = string.upcase

# Output the result
puts "Original String: #{string}"
puts "String in Uppercase: #{uppercase_string}"

Output
Original String: Geeks For Geeks
String in Uppercase: GEEKS FOR GEEKS

Using Capitalize

Capitalize is used to capitalize the first character of the string while converting the rest to lowercase.

Syntax:

string.capitalize

In the below example,the string first letter is capitalized and the rest to lowercase. After which we store the result in the variable and output the both the original and capitalized string.

Ruby
# Define a string
string = "GEEKS FOR GEEKS"

# Capitalize the string using capitalize method
capitalized_string = string.capitalize

# Output the result
puts "Original String: #{string}"
puts "Capitalized String: #{capitalized_string}"

Output
Original String: GEEKS FOR GEEKS
Capitalized String: Geeks for geeks

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads