Constructor and Description |
---|
WordUtils()
WordUtils instances should NOT be constructed in
standard programming. |
Modifier and Type | Method and Description |
---|---|
static String |
abbreviate(String str,
int lower,
int upper,
String appendToEnd)
Abbreviates the words nicely.
|
static String |
capitalize(String str)
Capitalizes all the whitespace separated words in a String.
|
static String |
capitalize(String str,
char... delimiters)
Capitalizes all the delimiter separated words in a String.
|
static String |
capitalizeFully(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 String |
capitalizeFully(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 boolean |
containsAllWords(CharSequence word,
CharSequence... words)
Checks if the String contains all words in the given array.
|
static String |
initials(String str)
Extracts the initial characters from each word in the String.
|
static String |
initials(String str,
char... delimiters)
Extracts the initial characters from each word in the String.
|
static boolean |
isDelimiter(char ch,
char[] delimiters)
Deprecated.
as of 1.2 and will be removed in 2.0
|
static boolean |
isDelimiter(int codePoint,
char[] delimiters)
Deprecated.
as of 1.2 and will be removed in 2.0
|
static String |
swapCase(String str)
Swaps the case of a String using a word based algorithm.
|
static String |
uncapitalize(String str)
Uncapitalizes all the whitespace separated words in a String.
|
static String |
uncapitalize(String str,
char... delimiters)
Uncapitalizes all the whitespace separated words in a String.
|
static String |
wrap(String str,
int wrapLength)
Wraps a single line of text, identifying words by
' ' . |
static String |
wrap(String str,
int wrapLength,
String newLineStr,
boolean wrapLongWords)
Wraps a single line of text, identifying words by
' ' . |
static String |
wrap(String str,
int wrapLength,
String newLineStr,
boolean wrapLongWords,
String wrapOn)
Wraps a single line of text, identifying words by
wrapOn . |
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 String abbreviate(String str, int lower, int upper, String appendToEnd)
This method searches for the first space after the lower limit and abbreviates the String there. It will also append any String passed as a parameter to the end of the String. The upper limit can be specified to forcibly abbreviate a String.
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; negative value is treated as zero.upper
- the upper limit; specify -1 if no limit is desired.
The upper limit cannot be lower than 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.WordUtils.abbreviate("Now is the time for all good men", 0, 40, null)); = "Now" WordUtils.abbreviate("Now is the time for all good men", 10, 40, null)); = "Now is the" WordUtils.abbreviate("Now is the time for all good men", 20, 40, null)); = "Now is the time for all" WordUtils.abbreviate("Now is the time for all good men", 0, 40, "")); = "Now" WordUtils.abbreviate("Now is the time for all good men", 10, 40, "")); = "Now is the" WordUtils.abbreviate("Now is the time for all good men", 20, 40, "")); = "Now is the time for all" WordUtils.abbreviate("Now is the time for all good men", 0, 40, " ...")); = "Now ..." WordUtils.abbreviate("Now is the time for all good men", 10, 40, " ...")); = "Now is the ..." WordUtils.abbreviate("Now is the time for all good men", 20, 40, " ...")); = "Now is the time for all ..." WordUtils.abbreviate("Now is the time for all good men", 0, -1, "")); = "Now" WordUtils.abbreviate("Now is the time for all good men", 10, -1, "")); = "Now is the" WordUtils.abbreviate("Now is the time for all good men", 20, -1, "")); = "Now is the time for all" WordUtils.abbreviate("Now is the time for all good men", 50, -1, "")); = "Now is the time for all good men" WordUtils.abbreviate("Now is the time for all good men", 1000, -1, "")); = "Now is the time for all good men" WordUtils.abbreviate("Now is the time for all good men", 9, -10, null)); = IllegalArgumentException WordUtils.abbreviate("Now is the time for all good men", 10, 5, null)); = IllegalArgumentException
public static String capitalize(String str)
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 String capitalize(String str, char... delimiters)
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" WordUtils.capitalize("i am fine", new char[]{}) = "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 String capitalizeFully(String str)
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 String capitalizeFully(String str, char... delimiters)
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 boolean containsAllWords(CharSequence word, CharSequence... words)
A null
String will return false
. A null
, zero
length search array or if one element of array is null will return false
.
WordUtils.containsAllWords(null, *) = false WordUtils.containsAllWords("", *) = false WordUtils.containsAllWords(*, null) = false WordUtils.containsAllWords(*, []) = false WordUtils.containsAllWords("abcd", "ab", "cd") = false WordUtils.containsAllWords("abc def", "def", "abc") = true
word
- The CharSequence to check, may be nullwords
- The array of String words to search for, may be nulltrue
if all search words are found, false
otherwisepublic static String initials(String str)
All first characters 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 String initials(String str, char... delimiters)
All first characters 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)
@Deprecated public static boolean isDelimiter(char ch, char[] delimiters)
ch
- the character to checkdelimiters
- the delimiters@Deprecated public static boolean isDelimiter(int codePoint, char[] delimiters)
codePoint
- the codePint to checkdelimiters
- the delimiterspublic static String swapCase(String str)
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 String uncapitalize(String str)
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 String uncapitalize(String str, char... delimiters)
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" WordUtils.uncapitalize("I am fine", new char[]{}) = "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 String wrap(String str, int wrapLength)
' '
.
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.
input | wrapLength | result |
---|---|---|
null | * | null |
"" | * | "" |
"Here is one line of text that is going to be wrapped after 20 columns." | 20 | "Here is one line of\ntext that is going\nto be wrapped after\n20 columns." |
"Click here to jump to the commons website - https://commons.apache.org" | 20 | "Click here to jump\nto the commons\nwebsite -\nhttps://commons.apache.org" |
"Click here, https://commons.apache.org, to jump to the commons website" | 20 | "Click here,\nhttps://commons.apache.org,\nto jump to the\ncommons website" |
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 String wrap(String str, int wrapLength, String newLineStr, boolean wrapLongWords)
' '
.
Leading spaces on a new line are stripped. Trailing spaces are not stripped.
input | wrapLength | newLineString | wrapLongWords | result |
---|---|---|---|---|
null | * | * | true/false | null |
"" | * | * | true/false | "" |
"Here is one line of text that is going to be wrapped after 20 columns." | 20 | "\n" | true/false | "Here is one line of\ntext that is going\nto be wrapped after\n20 columns." |
"Here is one line of text that is going to be wrapped after 20 columns." | 20 | "<br />" | true/false | "Here is one line of<br />text that is going< br />to be wrapped after<br />20 columns." |
"Here is one line of text that is going to be wrapped after 20 columns." | 20 | null | true/false | "Here is one line of" + systemNewLine + "text that is going" + systemNewLine + "to be wrapped after" + systemNewLine + "20 columns." |
"Click here to jump to the commons website - https://commons.apache.org" | 20 | "\n" | false | "Click here to jump\nto the commons\nwebsite -\nhttps://commons.apache.org" |
"Click here to jump to the commons website - https://commons.apache.org" | 20 | "\n" | true | "Click here to jump\nto the commons\nwebsite -\nhttp://commons.apach\ne.org" |
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 String wrap(String str, int wrapLength, String newLineStr, boolean wrapLongWords, String wrapOn)
wrapOn
.
Leading spaces on a new line are stripped. Trailing spaces are not stripped.
input | wrapLength | newLineString | wrapLongWords | wrapOn | result |
---|---|---|---|---|---|
null | * | * | true/false | * | null |
"" | * | * | true/false | * | "" |
"Here is one line of text that is going to be wrapped after 20 columns." | 20 | "\n" | true/false | " " | "Here is one line of\ntext that is going\nto be wrapped after\n20 columns." |
"Here is one line of text that is going to be wrapped after 20 columns." | 20 | "<br />" | true/false | " " | "Here is one line of<br />text that is going<br /> to be wrapped after<br />20 columns." |
"Here is one line of text that is going to be wrapped after 20 columns." | 20 | null | true/false | " " | "Here is one line of" + systemNewLine + "text that is going" + systemNewLine + "to be wrapped after" + systemNewLine + "20 columns." |
"Click here to jump to the commons website - https://commons.apache.org" | 20 | "\n" | false | " " | "Click here to jump\nto the commons\nwebsite -\nhttps://commons.apache.org" |
"Click here to jump to the commons website - https://commons.apache.org" | 20 | "\n" | true | " " | "Click here to jump\nto the commons\nwebsite -\nhttp://commons.apach\ne.org" |
"flammable/inflammable" | 20 | "\n" | true | "/" | "flammable\ninflammable" |
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 wrappedwrapOn
- regex expression to be used as a breakable characters,
if blank string is provided a space character will be usednull
if null inputCopyright © 2014–2022 The Apache Software Foundation. All rights reserved.