rubyguides

String#downcase

str.downcase -> string

String#downcase returns a new string with all characters converted to lowercase. It is useful for case-insensitive comparisons, normalizing user input, and formatting output for display.

This method does not modify the original string—it always returns a new string.

Syntax

string.downcase

Parameters

This method takes no parameters. It operates on all characters in the string, which keeps the API short and predictable. You call downcase on a string and get a lowercase copy back without having to configure the conversion.

Examples

The examples that follow cover the most common situations where converting to lowercase makes the code simpler. Each one shows how the method preserves the original string while returning a case-normalized copy.

Basic usage

"Hello World".downcase
# => "hello world"

"Ruby Programming".downcase
# => "ruby programming"

These examples show the simplest use of downcase: turn a string into a uniform lowercase form so later comparisons or displays do not have to worry about mixed casing. That small transformation often makes the rest of the code easier to read because the text is in a predictable state.

With Unicode characters

"ÇÀÂ".downcase
# => "çàâ"

"über".downcase
# => "über"

"Ångström".downcase
# => "ångström"

Unicode support matters because many applications handle text that is not plain ASCII. The method keeps those characters usable after the conversion, which makes it safe to apply in international input pipelines without needing separate case tables in the calling code.

Working with special characters

"Numbers: 123".downcase
# => "numbers: 123"

"Symbols: !@#".downcase
# => "symbols: !@#"

Special characters are left alone because the method only changes letters. That behavior is useful when a string mixes words with numbers or symbols, since the readable parts are normalized while the rest of the content stays intact.

Common patterns

Case-insensitive comparison

When comparing strings without regard to case, convert both to lowercase first:

username = "JohnDoe"
username.downcase == "johndoe"
# => true

Case-insensitive comparisons are one of the most common reasons to use this method. By normalizing both sides first, the comparison becomes simple and the surrounding code does not need to repeat case handling in multiple places.

Normalizing user input

Many applications normalize user input to ensure consistent storage:

def normalize_name(name)
  name.strip.downcase
end

normalize_name("  JOHN ")
# => "john"

normalize_name("ALICE")
# => "alice"

Combining strip and downcase is a common pattern because it removes both spacing noise and case differences in one step. That keeps the input pipeline tidy and gives later code a value that is already in a consistent form.

Unicode support

Ruby handles Unicode properly. The method respects Unicode character properties, so it works correctly with international text. The Turkish dotted and dotless i problem is handled correctly in Ruby 2.5+.

That Unicode behavior makes the method useful in real applications that serve more than one language. Instead of needing special-case code at every call site, you can rely on the string API to do the conversion in a predictable way.

Practical notes

downcase is often used as a normalization step before comparisons, lookups, or storage. That makes case-insensitive logic much easier to read because the comparison happens on a stable form of the string instead of repeating conditional case handling throughout the code.

When dealing with user input, it is common to combine strip and downcase so that both spacing and casing are normalized in one place. That keeps the rest of the program from having to think about formatting noise.

Downcase with comparison shortcuts

Ruby provides a few ways to shorten case-insensitive comparisons. While downcase is the most explicit approach, some alternatives exist for specific situations:

# Case-insensitive pattern matching with regex
"Hello World".match?(/hello world/i)   # => true

# Casecmp for simple equality checks
"Hello".casecmp("hello")               # => 0 (meaning equal)
"Hello".casecmp?("hello")              # => true (Ruby 2.4+)

The casecmp? method (Ruby 2.4+) is a concise way to compare strings without case sensitivity, and it is often faster than calling downcase on both sides. However, downcase remains the better choice when you need the normalized string for more than one comparison, because it gives you a reusable value instead of repeating the normalization at every check.

See Also