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.
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.
StringUtils
You can import the StringUtils
class as follows.
import org.apache.commons.lang3.StringUtils;
public static String capitalize(final String str)
final String str
is the string to be capitalized.
The method capitalize()
returns the capitalized string.
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'"));}}
Ababaaaabbbcccac
'hello'
The second string remains the same as the input string, and the first character remains a single quote.