What is StringUtils.capitalize in Java?
capitalize is a static method of the StringUtils class that is used to convert the first character of the given string to title case as per Character.toTitleCase. The remaining characters of the string are not changed.
For example:
string = "ababaaaabbbcccac"
capitalize returns Ababaaaabbbcccac, changing the case of the first character to title case.
Add apache commons-lang package
StringUtils is defined in the Apache Commons Lang package. Apache Commons Lang can be added 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>
Note: For other versions of the commons-lang package, refer to the Maven Repository.
Import StringUtils
You can import the StringUtils class as follows.
import org.apache.commons.lang3.StringUtils;
Syntax
public static String capitalize(final String str)
Parameters
final String str is the string to be capitalized.
Returns
The method capitalize() returns the capitalized string.
Code
import org.apache.commons.lang3.StringUtils;public class Main {public static void main(String[] args) {System.out.println(StringUtils.capitalize("ababaaaabbbcccac"));System.out.println(StringUtils.capitalize("'hello'"));}}
Output
Ababaaaabbbcccac
'hello'
The second string remains the same as the input string, and the first character remains a single quote.