Home >Java >javaTutorial >How Can I Capitalize the First Letter of Each Word in a Java String?
Capitalizing Initial Characters in String Words with Built-in Java Function
In Java, the built-in function WordUtils.capitalize(str) from the Apache Commons Text library can capitalize the first character of each word in a given string while leaving other characters unaffected.
Usage:
To capitalize the first character of each word in a string, simply use the following syntax:
String capitalizedString = WordUtils.capitalize(originalString);
Examples:
Consider the following examples to illustrate the behavior of the WordUtils.capitalize() function:
Note:
If you wish to capitalize the first character of every word, including conjunctions like "and" and "the," you can use the WordUtils.capitalizeFully(..) function instead. This will result in the following output for the given example:
The above is the detailed content of How Can I Capitalize the First Letter of Each Word in a Java String?. For more information, please follow other related articles on the PHP Chinese website!