What is WordUtils.capitalizeFully in Java?
What is capitalizeFully()?
capitalizeFully is a static method of the WordUtils class that is used to convert every delimiter-separated word in a given string to capitalized words. These capitalized words contain a title case character followed by a sequence of lowercase letters.
Arguments
-
The set of delimiters that separate the words can be passed to the method.
-
If no delimiters are specified, whitespace is considered to be the delimiter by default.
How to import WordUtils
WordUtils is defined in the Apache Commons Text package. Apache Commons Text 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-text</artifactId>
<version>1.9</version>
</dependency>
For other versions of the
commons-textpackage, refer to the Maven Repository.
You can import the WordUtils class as follows.
import org.apache.commons.text.WordUtils;
Syntax
public static String capitalizeFully(String str, final char... delimiters)
Parameters
final String str: string to be capitalized.final char... delimiters: set of delimiters.
Return value
This method returns a new capitalized string.
Code
Example 1
- string:
"Hello educative how are you?" - delimiters:
[]
Application of the capitalizeFully function will result in: Hello Educative How Are You?.
All the words are capitalized that are separated by whitespace.
Example 2
- string:
"lorem ipsum is a pseudo-Latin text used in web design,typography,layout,and printing in place of english to emphasise design elements over content.it's also called placeholder (or filler) text." - delimiters:
[',', '.']
Application of the capitalizeFully function will result in:
"Lorem ipsum is a pseudo-latin text used in web design,Typography,Layout,And printing in place of english to emphasise design elements over content.It's also called placeholder (or filler) text."
Here, the function capitalizes the first character of each fragment, separated by a comma or a full-stop.
import org.apache.commons.text.WordUtils;public class Main {public static void main(String[] args) {String string = "Hello educative how are you?";System.out.print("Delimited by whitespace - ");System.out.print(WordUtils.capitalizeFully(string));string = "lorem ipsum is a pseudo-Latin text used in web design,typography,layout,and printing in place of english to emphasise design elements over content.it's also called placeholder (or filler) text.";System.out.print("\nDelimited by comma (, and .) - ");System.out.print(WordUtils.capitalizeFully(string, ',', '.'));}}
Output
Delimited by whitespace - Hello Educative How Are You?
Delimited by comma (, and .) - Lorem ipsum is a pseudo-latin text used in web design,Typography,Layout,And printing in place of english to emphasise design elements over content.It's also called placeholder (or filler) text.