Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java 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.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M