What is StringUtils.lowerCase in Java?
Overview
lowerCase() is a StringUtils which is used to convert a string to lowercase. This method optionally takes a locale. If the locale is not specified, then the default locale of the system is taken. The method returns null if the input string is null.
How to import StringUtils
The definition of StringUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
For other versions of the commons-lang package, refer to the Maven Repository.
You can import the StringUtils class as follows:
import org.apache.commons.lang3.StringUtils;
Syntax
public static String lowerCase(final String str, final Locale locale)
Parameters
final String str: The string to convert to lowercase.final Locale locale: The locale that defines the case transformation rules.
Return value
This method returns a lowercase string.
Overloaded Methods
public static String lowerCase(final String str)
Code
import org.apache.commons.lang3.StringUtils;import java.util.Locale;public class Main {public static void main(String[] args) {String s = "HellO-EDUcative";System.out.printf("The output of StringUtils.lowerCase() for the string - '%s' is %s", s, StringUtils.lowerCase(s, Locale.ENGLISH));System.out.println();s = "";System.out.printf("The output of StringUtils.lowerCase() for the string - '%s' is %s", s, StringUtils.lowerCase(s, Locale.ENGLISH));System.out.println();s = null;System.out.printf("The output of StringUtils.lowerCase() for the string - '%s' is %s", s, StringUtils.lowerCase(s, Locale.ENGLISH));System.out.println();}}
Example 1
string = "HellO-EDUcative"locale = english
The method returns hello-educative when the string is converted to lowercase.
Example 2
string = ""locale = english
The method returns `` as the input string is empty.
Example 3
string = nulllocale = english
The method returns null as the input string is null.
Output
The output of the code will be as follows:
The output of StringUtils.lowerCase() for the string - 'HellO-EDUcative' is hello-educative
The output of StringUtils.lowerCase() for the string - '' is
The output of StringUtils.lowerCase() for the string - 'null' is null