0
0
Rubyprogramming~5 mins

String methods (upcase, downcase, strip) in Ruby

Choose your learning style9 modes available
Introduction

These methods help change or clean text easily. You can make all letters uppercase, lowercase, or remove extra spaces.

When you want to make a name all uppercase for a title.
When you need to compare words without worrying about uppercase or lowercase differences.
When you get input from a user and want to remove spaces before or after the text.
When formatting text before saving it to a file or database.
When cleaning data to avoid errors caused by extra spaces.
Syntax
Ruby
string.upcase
string.downcase
string.strip

upcase makes all letters uppercase.

downcase makes all letters lowercase.

strip removes spaces at the start and end of the string.

Examples
This changes all letters to uppercase.
Ruby
"hello".upcase # => "HELLO"
This changes all letters to lowercase.
Ruby
"WORLD".downcase # => "world"
This removes spaces before and after the word.
Ruby
"  ruby  ".strip # => "ruby"
Sample Program

This program shows how the three methods change the string name. It prints the original string, then the uppercase, lowercase, and stripped versions.

Ruby
name = "  Alice "
puts "Original: '#{name}'"
puts "Upcase: '#{name.upcase}'"
puts "Downcase: '#{name.downcase}'"
puts "Strip: '#{name.strip}'"
OutputSuccess
Important Notes

These methods do not change the original string unless you add an exclamation mark, like upcase!.

Use strip to clean user input before processing.

Summary

upcase makes all letters uppercase.

downcase makes all letters lowercase.

strip removes spaces at the start and end of a string.