Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Ruby Dictionary

  1. Home
  2. Ruby Dictionary
  3. String.upcase / downcase / capitalize / swapcase

String.upcase / downcase / capitalize / swapcase

Methods that convert the alphabetic characters of a string to uppercase or lowercase. Japanese characters are not affected.

Syntax

# Converts all characters to uppercase.
string.upcase

# Converts all characters to lowercase.
string.downcase

# Capitalizes only the first character.
string.capitalize

# Swaps the case of each character.
string.swapcase

Method List

MethodDescription
upcaseReturns a new string with all alphabetic characters converted to uppercase.
downcaseReturns a new string with all alphabetic characters converted to lowercase.
capitalizeReturns a new string with the first character uppercased and the rest lowercased.
swapcaseReturns a new string with uppercase letters converted to lowercase and vice versa.
upcase!Modifies the original string in place (destructive method).
downcase!Modifies the original string in place (destructive method).

Sample Code

text = "Hello, Ruby World!"

# Converts to uppercase.
puts text.upcase      # HELLO, RUBY WORLD!

# Converts to lowercase.
puts text.downcase    # hello, ruby world!

# Capitalizes only the first character.
puts "hello world".capitalize  # Hello world

# Swaps the case of each character.
puts text.swapcase    # hELLO, rUBY wORLD!

# The original string is unchanged.
puts text  # Hello, Ruby World!

# Useful for case-insensitive comparisons.
input = "Yes"
if input.downcase == "yes"
  puts "You answered yes."  # This line is printed.
end

# Normalize user input before processing.
user_code = "ABC-123"
normalized = user_code.upcase
puts normalized  # ABC-123

Overview

All of these methods convert the case of alphabetic characters. Japanese characters, symbols, and other non-alphabetic characters are not affected. The original string is not modified — each method returns a new string with the converted result.

Note that capitalize uppercases the first character and lowercases all remaining characters. For example, "hELLO".capitalize returns "Hello". For case-insensitive string comparisons, convert both strings with downcase or upcase before comparing.

To replace parts of a string, use sub / gsub. To remove whitespace, use strip / chomp.

If you find any errors or copyright issues, please .