public class WordUtils
extends java.lang.Object
Operations on Strings that contain words.
This class tries to handle null
input gracefully.
An exception will not be thrown for a null
input.
Each method documents its behaviour in more detail.
Constructor and Description |
---|
WordUtils()
WordUtils instances should NOT be constructed in
standard programming. |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
abbreviate(java.lang.String str,
int lower,
int upper,
java.lang.String appendToEnd)
Abbreviates a string nicely.
|
static java.lang.String |
capitalize(java.lang.String str)
Capitalizes all the whitespace separated words in a String.
|
static java.lang.String |
capitalize(java.lang.String str,
char[] delimiters)
Capitalizes all the delimiter separated words in a String.
|
static java.lang.String |
capitalizeFully(java.lang.String str)
Converts all the whitespace separated words in a String into capitalized words,
that is each word is made up of a titlecase character and then a series of
lowercase characters.
|
static java.lang.String |
capitalizeFully(java.lang.String str,
char[] delimiters)
Converts all the delimiter separated words in a String into capitalized words,
that is each word is made up of a titlecase character and then a series of
lowercase characters.
|
static java.lang.String |
initials(java.lang.String str)
Extracts the initial letters from each word in the String.
|
static java.lang.String |
initials(java.lang.String str,
char[] delimiters)
Extracts the initial letters from each word in the String.
|
static java.lang.String |
swapCase(java.lang.String str)
Swaps the case of a String using a word based algorithm.
|
static java.lang.String |
uncapitalize(java.lang.String str)
Uncapitalizes all the whitespace separated words in a String.
|
static java.lang.String |
uncapitalize(java.lang.String str,
char[] delimiters)
Uncapitalizes all the whitespace separated words in a String.
|
static java.lang.String |
wrap(java.lang.String str,
int wrapLength)
Wraps a single line of text, identifying words by
' ' . |
static java.lang.String |
wrap(java.lang.String str,
int wrapLength,
java.lang.String newLineStr,
boolean wrapLongWords)
Wraps a single line of text, identifying words by
' ' . |
public WordUtils()
WordUtils
instances should NOT be constructed in
standard programming. Instead, the class should be used as
WordUtils.wrap("foo bar", 20);
.
This constructor is public to permit tools that require a JavaBean instance to operate.
public static java.lang.String wrap(java.lang.String str, int wrapLength)
Wraps a single line of text, identifying words by ' '
.
New lines will be separated by the system property line separator. Very long words, such as URLs will not be wrapped.
Leading spaces on a new line are stripped. Trailing spaces are not stripped.
WordUtils.wrap(null, *) = null WordUtils.wrap("", *) = ""
str
- the String to be word wrapped, may be nullwrapLength
- the column to wrap the words at, less than 1 is treated as 1null
if null inputpublic static java.lang.String wrap(java.lang.String str, int wrapLength, java.lang.String newLineStr, boolean wrapLongWords)
Wraps a single line of text, identifying words by ' '
.
Leading spaces on a new line are stripped. Trailing spaces are not stripped.
WordUtils.wrap(null, *, *, *) = null WordUtils.wrap("", *, *, *) = ""
str
- the String to be word wrapped, may be nullwrapLength
- the column to wrap the words at, less than 1 is treated as 1newLineStr
- the string to insert for a new line,
null
uses the system property line separatorwrapLongWords
- true if long words (such as URLs) should be wrappednull
if null inputpublic static java.lang.String capitalize(java.lang.String str)
Capitalizes all the whitespace separated words in a String.
Only the first letter of each word is changed. To convert the
rest of each word to lowercase at the same time,
use capitalizeFully(String)
.
Whitespace is defined by Character.isWhitespace(char)
.
A null
input String returns null
.
Capitalization uses the unicode title case, normally equivalent to
upper case.
WordUtils.capitalize(null) = null WordUtils.capitalize("") = "" WordUtils.capitalize("i am FINE") = "I Am FINE"
str
- the String to capitalize, may be nullnull
if null String inputuncapitalize(String)
,
capitalizeFully(String)
public static java.lang.String capitalize(java.lang.String str, char[] delimiters)
Capitalizes all the delimiter separated words in a String.
Only the first letter of each word is changed. To convert the
rest of each word to lowercase at the same time,
use capitalizeFully(String, char[])
.
The delimiters represent a set of characters understood to separate words. The first string character and the first non-delimiter character after a delimiter will be capitalized.
A null
input String returns null
.
Capitalization uses the unicode title case, normally equivalent to
upper case.
WordUtils.capitalize(null, *) = null WordUtils.capitalize("", *) = "" WordUtils.capitalize(*, new char[0]) = * WordUtils.capitalize("i am fine", null) = "I Am Fine" WordUtils.capitalize("i aM.fine", {'.'}) = "I aM.Fine"
str
- the String to capitalize, may be nulldelimiters
- set of characters to determine capitalization, null means whitespacenull
if null String inputuncapitalize(String)
,
capitalizeFully(String)
public static java.lang.String capitalizeFully(java.lang.String str)
Converts all the whitespace separated words in a String into capitalized words, that is each word is made up of a titlecase character and then a series of lowercase characters.
Whitespace is defined by Character.isWhitespace(char)
.
A null
input String returns null
.
Capitalization uses the unicode title case, normally equivalent to
upper case.
WordUtils.capitalizeFully(null) = null WordUtils.capitalizeFully("") = "" WordUtils.capitalizeFully("i am FINE") = "I Am Fine"
str
- the String to capitalize, may be nullnull
if null String inputpublic static java.lang.String capitalizeFully(java.lang.String str, char[] delimiters)
Converts all the delimiter separated words in a String into capitalized words, that is each word is made up of a titlecase character and then a series of lowercase characters.
The delimiters represent a set of characters understood to separate words. The first string character and the first non-delimiter character after a delimiter will be capitalized.
A null
input String returns null
.
Capitalization uses the unicode title case, normally equivalent to
upper case.
WordUtils.capitalizeFully(null, *) = null WordUtils.capitalizeFully("", *) = "" WordUtils.capitalizeFully(*, null) = * WordUtils.capitalizeFully(*, new char[0]) = * WordUtils.capitalizeFully("i aM.fine", {'.'}) = "I am.Fine"
str
- the String to capitalize, may be nulldelimiters
- set of characters to determine capitalization, null means whitespacenull
if null String inputpublic static java.lang.String uncapitalize(java.lang.String str)
Uncapitalizes all the whitespace separated words in a String. Only the first letter of each word is changed.
Whitespace is defined by Character.isWhitespace(char)
.
A null
input String returns null
.
WordUtils.uncapitalize(null) = null WordUtils.uncapitalize("") = "" WordUtils.uncapitalize("I Am FINE") = "i am fINE"
str
- the String to uncapitalize, may be nullnull
if null String inputcapitalize(String)
public static java.lang.String uncapitalize(java.lang.String str, char[] delimiters)
Uncapitalizes all the whitespace separated words in a String. Only the first letter of each word is changed.
The delimiters represent a set of characters understood to separate words. The first string character and the first non-delimiter character after a delimiter will be uncapitalized.
Whitespace is defined by Character.isWhitespace(char)
.
A null
input String returns null
.
WordUtils.uncapitalize(null, *) = null WordUtils.uncapitalize("", *) = "" WordUtils.uncapitalize(*, null) = * WordUtils.uncapitalize(*, new char[0]) = * WordUtils.uncapitalize("I AM.FINE", {'.'}) = "i AM.fINE"
str
- the String to uncapitalize, may be nulldelimiters
- set of characters to determine uncapitalization, null means whitespacenull
if null String inputcapitalize(String)
public static java.lang.String swapCase(java.lang.String str)
Swaps the case of a String using a word based algorithm.
Whitespace is defined by Character.isWhitespace(char)
.
A null
input String returns null
.
StringUtils.swapCase(null) = null StringUtils.swapCase("") = "" StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
str
- the String to swap case, may be nullnull
if null String inputpublic static java.lang.String initials(java.lang.String str)
Extracts the initial letters from each word in the String.
The first letter of the string and all first letters after whitespace are returned as a new string. Their case is not changed.
Whitespace is defined by Character.isWhitespace(char)
.
A null
input String returns null
.
WordUtils.initials(null) = null WordUtils.initials("") = "" WordUtils.initials("Ben John Lee") = "BJL" WordUtils.initials("Ben J.Lee") = "BJ"
str
- the String to get initials from, may be nullnull
if null String inputinitials(String,char[])
public static java.lang.String initials(java.lang.String str, char[] delimiters)
Extracts the initial letters from each word in the String.
The first letter of the string and all first letters after the defined delimiters are returned as a new string. Their case is not changed.
If the delimiters array is null, then Whitespace is used.
Whitespace is defined by Character.isWhitespace(char)
.
A null
input String returns null
.
An empty delimiter array returns an empty String.
WordUtils.initials(null, *) = null WordUtils.initials("", *) = "" WordUtils.initials("Ben John Lee", null) = "BJL" WordUtils.initials("Ben J.Lee", null) = "BJ" WordUtils.initials("Ben J.Lee", [' ','.']) = "BJL" WordUtils.initials(*, new char[0]) = ""
str
- the String to get initials from, may be nulldelimiters
- set of characters to determine words, null means whitespacenull
if null String inputinitials(String)
public static java.lang.String abbreviate(java.lang.String str, int lower, int upper, java.lang.String appendToEnd)
str
- the string to be abbreviated. If null is passed, null is returned.
If the empty String is passed, the empty string is returned.lower
- the lower limit.upper
- the upper limit; specify -1 if no limit is desired.
If the upper limit is lower than the lower limit, it will be
adjusted to be the same as the lower limit.appendToEnd
- String to be appended to the end of the abbreviated string.
This is appended ONLY if the string was indeed abbreviated.
The append does not count towards the lower or upper limits."Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"