Java Reference Library →

String toLowerCase() method - Syntax and Examples

String toLowerCase() method - Syntax and Examples

Learn about the Java String `toLowerCase()` method. This tutorial covers its syntax, parameters, return values, and provides practical examples.

This reference explains String toLowerCase() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

String toLowerCase() method - Syntax and Examples

Introduction

Sometimes you need to work with strings where case doesn't matter. Maybe you're comparing user input to a list of valid options, or sorting names alphabetically regardless of capitalization. The `toLowerCase()` method in Java provides a straightforward way to convert all characters in a string to their lowercase equivalents.

Syntax

public String toLowerCase()
public String toLowerCase(Locale locale)

Parameters

ParameterDescription
localeOptional. A Locale object that defines the rules for lowercase conversion based on a specific language and region. If omitted, the default locale of the system is used.

Return Value

The `toLowerCase()` method returns a new string where all characters have been converted to their lowercase representations. The original string remains unchanged.

Examples

Example 1: Basic Usage

This example demonstrates the simplest use of the `toLowerCase()` method, without specifying a locale.

String str = "Hello World";
String lowerCaseStr = str.toLowerCase();
System.out.println(lowerCaseStr);
hello world

Here, the `toLowerCase()` method converts all uppercase characters in the string "Hello World" to lowercase, resulting in the new string "hello world". The original string str is not modified.

Example 2: Using a Locale

This example shows how you can use a specific locale for the conversion. Different locales handle certain characters differently; this becomes important when dealing with languages other than English where lowercase transformations are not straightforward.

import java.util.Locale;

String str = "ßöäü"; // German characters
String lowerCaseStrDefault = str.toLowerCase();
String lowerCaseStrGerman = str.toLowerCase(Locale.GERMAN);

System.out.println("Default Locale: " + lowerCaseStrDefault);
System.out.println("German Locale: " + lowerCaseStrGerman);
Default Locale: ßöäüGerman Locale: ssöäü

In this case, the default locale might not properly convert the German characters (ß, ö, ä, ü) to their lowercase equivalents. Specifying Locale.GERMAN ensures that these characters are converted correctly.

Example 3: Case-Insensitive Comparison

This example demonstrates how `toLowerCase()` is often used in case-insensitive comparisons.

String userInput = "Java";
String validOption = "java";

boolean match = userInput.toLowerCase().equals(validOption.toLowerCase());

System.out.println("Match: " + match);
Match: true

To compare `userInput` and `validOption` without considering case, both strings are first converted to lowercase using `toLowerCase()`. Then, the `equals()` method is used to check if the lowercased versions are equal. This ensures that the comparison is not affected by capitalization differences.

Example 4: String Manipulation

This example shows how you might use this in a more complex string manipulation scenario, like creating a search term for a database query.

String searchTerm = "Find all documents about Apples and oranges";
String lowercaseSearchTerm = searchTerm.toLowerCase();
System.out.println("Lowercase Search Term: " + lowercaseSearchTerm);
Lowercase Search Term: find all documents about apples and oranges

By converting the search term to lowercase, you can ensure that your database query is case-insensitive, retrieving results regardless of whether the content in the database uses uppercase or lowercase letters.

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →