⬅ Previous Topic
Java StringtoCharArray()
methodNext Topic ⮕
Java StringtoString()
methodtoLowerCase()
method⬅ Previous Topic
Java StringtoCharArray()
methodNext Topic ⮕
Java StringtoString()
methodSometimes 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.
public String toLowerCase()
public String toLowerCase(Locale locale)
Parameter | Description |
---|---|
locale | Optional. 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. |
The `toLowerCase()` method returns a new string where all characters have been converted to their lowercase representations. The original string remains unchanged.
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.
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.
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.
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.
⬅ Previous Topic
Java StringtoCharArray()
methodNext Topic ⮕
Java StringtoString()
methodYou 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.