Home  >  Article  >  Java  >  Java String to Lowercase

Java String to Lowercase

王林
王林Original
2024-08-30 15:13:11498browse

Java provides some packages & methods to work with the string. Using these methods, the string can be converted into different formats as it is needed depending on the requirement.  Java string class has a method toLowerCase() to convert the string into lowercase.

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

String class in java provides a method toLowerCase()  which converts all characters of the string tolowercase. toLowerCase() uses the default parameter “Local.getDefault()” when nothing is specified as the parameter explicitly. This method should be used carefully as it is Local sensitive otherwise HTML tags, protocol keys, programming language identifiers can generate anonymous characters. toLowerCase() creates a new string after converting it to lowercase characters.

Syntax:

In the following syntax, toLowerCase() method is given, which converts a string tolowercase.

toLowerCase()
//OR
public String toLowerCase(Locale locale)

How to Replace String to lowercase?

Replacing strings tolowercase can be done in multiple ways. One of the best ways to convert the string into lowercase is using toLowerCase(). toLowerCase() is a public method.

Method toLowerCase() is similar to toLowerCase(Locale.getDefault()). getDeafult() gets the value of default Locale for this instance of the JVM. Default Locale is set by JVM at the time of startup. If locale is not specified explicitly. It can be set explicitly using the method setDefault().

The locale is required for the operation to perform its task. This type of operation is known as locale-sensitive.  While passing null as a locale parameter then the program will throw NullPointerException.

Examples of Java String to Lowercase

Here are the following examples mentioned below:

Example #1

In this example, the first line of the main method instantiated an object to take input from the user. After that displaying a message to enter an input sentence. In the next line, the input string is stored in the variable “inputStr”. The last line displays the converted string in lowercase.

//importing packages here
import java.util.*;
class StringToLowercaseExample{
public static void main(String[] args) {
//instantiating scanner object
Scanner scObj = new Scanner(System.in);
System.out.println("Please enter String to convert it into lowercase:");
// retrieving string entered
String inputStr = scObj.nextLine();
//converting string into lowercase
System.out.println("String after conversion in Lower Case = " + inputStr.toLowerCase());
}
}

Output:

Java String to Lowercase

Example #2

In this example, displaying two strings, the first string is capitalized & second has most of the word in camelCase. After applying the method toLowerCase()  string converted into lowercase as given in the output screenshot.

//importing packages here
import java.util.*;
class StringToLowercaseExample2{
public static void main(String[] args) {
//assigning string to the first variable
String strFirst = "FAMILIARITY BREEDS CONTEMPT.";
//converting I string to lowercase
String strFirstLowerCase = strFirst.toLowerCase();
//displaying string after conversion
System.out.println("String I after converting into lowercase: \n" + strFirstLowerCase);
//assigning string to the second variable
String strSecond = "Every Cloud has a Silver Lining.";
//converting II string to lowercase
String strSecondLowerCase = strSecond.toLowerCase();
//displaying string after conversion
System.out.println("\nString II after converting into lowercase: \n" + strSecondLowerCase);
}
}

Output:

Java String to Lowercase

Example #3

In this example, three separate strings are converted into the lowercase, each string has a different parameter in locale i.e. ENGLISH, FRANCE, CHINESE.

//importing packages here
import java.util.*;
class StringToLowercaseExample3{
public static void main(String[] args) {
//assigning string to a variable
String str1 = "There is No Place Like Home";
//displaying the str1 before conversion
System.out.println(str1);
//converting string to lowercase, specifying Locale explicitly
String str1Converted = str1.toLowerCase(Locale.FRANCE);
//displaying the str1 after conversion
System.out.println(str1Converted);
//line separator
System.out.println("\n");
String str2 = "No Man is an Island";
//displaying the str2 before conversion
System.out.println(str2);
//converting string to lowercase, specifying Locale explicitly
String str2Converted = str2.toLowerCase(Locale.ENGLISH);
//String turkish = str2.toLowerCase(Locale.forLanguageTag("tr"));
//displaying the str2 after conversion
System.out.println(str2Converted);
//line separator
System.out.println("\n");
String str3 = "An Empty Vessel Makes Much Noise";
//displaying the str3 before conversion
System.out.println(str3);
//converting string to lowercase, specifying Locale explicitly
String str3Converted = str3.toLowerCase(Locale.CHINA);
//displaying the str3 after conversion
System.out.println(str3Converted);
}
}

Output:

Java String to Lowercase

Conclusion

In this article, we go through the package &  method which is provided by the java class to convert strings into lowercase. Also gone through the default locale parameter, toLowerCase() method takes it implicitly if nothing is specified as the parameter. Given examples also explain how toLowerCase() method can be used in the program to convert a string into lowercase.

The above is the detailed content of Java String to Lowercase. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:String in JavaNext article:String in Java