Class StringUtils
Operations on String that are null safe.
- IsEmpty/IsBlank - checks if a String contains text
- Trim - removes leading and trailing whitespace
- Equals/Compare - compares two strings null-safe
- Substring - null-safe substring extraction
- UpperCase/LowerCase/Capitalize/Uncapitalize - changes the case of a String
The StringUtils class defines certain words related to
String handling.
- null -
null - empty - a zero-length string (
"") - space - the space character (
' ', char 32) - whitespace - the characters defined by
Character.isWhitespace(char) - trim - the characters <= 32 as in
String.trim()
StringUtils handles null input Strings quietly.
That is to say that a null input will return null.
Where a boolean or int is being returned
details vary by method.
A side effect of the null handling is that a
NullPointerException should be considered a bug in
StringUtils.
This class's source was modified from the Apache commons-lang and Apache commons-text libraries: https://github.com/apache/commons-lang/, https://github.com/apache/commons-text/
#ThreadSafe#
-
Method Summary
Modifier and TypeMethodDescriptionstatic Stringcapitalize(String str) Capitalizes a String changing the first character to title case as perCharacter.toTitleCase(int).static booleanCompares two Strings, returningtrueif they represent equal sequences of characters.static StringescapeJavaString(Object object, String indent) static StringIndents each line in the provided string by the provided number of spaces.static booleanisBlank(CharSequence cs) Checks if a CharSequence is empty (""), null or whitespace only.static booleanisEmpty(CharSequence cs) Checks if a CharSequence is empty ("") or null.static booleanChecks if a CharSequence is not empty (""), not null and not whitespace only.static StringLeft pad a String with spaces (' ').static StringLeft pad a String with a specified character.static StringLeft pad a String with a specified String.static intlevenshteinDistance(CharSequence left, CharSequence right, int threshold) Find the Levenshtein distance between two CharSequences if it's less than or equal to a given threshold.static StringConverts a String to lower case as perString.toLowerCase().static Stringrepeat(char ch, int repeat) Returns padding using the specified delimiter repeated to a given length.static StringRepeat a Stringrepeattimes to form a new String.static StringRight pad a String with spaces (' ').static StringRight pad a String with a specified character.static StringRight pad a String with a specified String.static booleanstartsWithIgnoreCase(String str, String prefix) Tests if this string starts with the specified prefix ignoring case considerations.static StringStrips any of a set of characters from the start and end of a String.static StringStrips any of a set of characters from the end of a String.static StringstripStart(String str, String stripChars) Strips any of a set of characters from the start of a String.static StringstripToEmpty(String str) Strips whitespace from the start and end of a String returning an empty String ifnullinput.static StringGets a substring from the specified String avoiding exceptions.static StringGets a substring from the specified String avoiding exceptions.static StringRemoves control characters (char <= 32) from both ends of this String, handlingnullby returningnull.static StringtrimToEmpty(String str) Removes control characters (char <= 32) from both ends of this String returning an empty String ("") if the String is empty ("") after the trim or if it isnull.static StringtrimToNull(String str) Removes control characters (char <= 32) from both ends of this String returningnullif the String is empty ("") after the trim or if it isnull.static Stringuncapitalize(String str) Uncapitalizes a String, changing the first character to lower case as perCharacter.toLowerCase(int).static StringConverts a String to upper case as perString.toUpperCase().static StringWraps a single line of text, identifying words by' '.static StringWraps a single line of text, identifying words by' '.static StringWraps a single line of text, identifying words bywrapOn.
-
Method Details
-
isEmpty
Checks if a CharSequence is empty ("") or null.
StringUtils.isEmpty(null) = true StringUtils.isEmpty("") = true StringUtils.isEmpty(" ") = false StringUtils.isEmpty("bob") = false StringUtils.isEmpty(" bob ") = falseNOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is available in isBlank().
- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif the CharSequence is empty or null- Since:
- 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
- See Also:
-
isBlank
Checks if a CharSequence is empty (""), null or whitespace only.
Whitespace is defined by
Character.isWhitespace(char).StringUtils.isBlank(null) = true StringUtils.isBlank("") = true StringUtils.isBlank(" ") = true StringUtils.isBlank("bob") = false StringUtils.isBlank(" bob ") = false- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif the CharSequence is null, empty or whitespace only- Since:
- 2.0, 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
- See Also:
-
isNotBlank
Checks if a CharSequence is not empty (""), not null and not whitespace only.
Whitespace is defined by
Character.isWhitespace(char).StringUtils.isNotBlank(null) = false StringUtils.isNotBlank("") = false StringUtils.isNotBlank(" ") = false StringUtils.isNotBlank("bob") = true StringUtils.isNotBlank(" bob ") = true- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif the CharSequence is not empty and not null and not whitespace only- Since:
- 2.0, 3.0 Changed signature from isNotBlank(String) to isNotBlank(CharSequence)
- See Also:
-
trim
Removes control characters (char <= 32) from both ends of this String, handling
nullby returningnull.The String is trimmed using
String.trim(). Trim removes start and end characters <= 32.StringUtils.trim(null) = null StringUtils.trim("") = "" StringUtils.trim(" ") = "" StringUtils.trim("abc") = "abc" StringUtils.trim(" abc ") = "abc"- Parameters:
str- the String to be trimmed, may be null- Returns:
- the trimmed string,
nullif null String input - See Also:
-
trimToNull
Removes control characters (char <= 32) from both ends of this String returning
nullif the String is empty ("") after the trim or if it isnull.The String is trimmed using
String.trim(). Trim removes start and end characters <= 32.StringUtils.trimToNull(null) = null StringUtils.trimToNull("") = null StringUtils.trimToNull(" ") = null StringUtils.trimToNull("abc") = "abc" StringUtils.trimToNull(" abc ") = "abc"- Parameters:
str- the String to be trimmed, may be null- Returns:
- the trimmed String,
nullif only chars <= 32, empty or null String input - Since:
- 2.0
- See Also:
-
trimToEmpty
Removes control characters (char <= 32) from both ends of this String returning an empty String ("") if the String is empty ("") after the trim or if it is
null.The String is trimmed using
String.trim(). Trim removes start and end characters <= 32.StringUtils.trimToEmpty(null) = "" StringUtils.trimToEmpty("") = "" StringUtils.trimToEmpty(" ") = "" StringUtils.trimToEmpty("abc") = "abc" StringUtils.trimToEmpty(" abc ") = "abc"- Parameters:
str- the String to be trimmed, may be null- Returns:
- the trimmed String, or an empty String if
nullinput - Since:
- 2.0
- See Also:
-
equals
Compares two Strings, returning
trueif they represent equal sequences of characters.nulls are handled without exceptions. Twonullreferences are considered to be equal. The comparison is case sensitive.StringUtils.equals(null, null) = true StringUtils.equals(null, "abc") = false StringUtils.equals("abc", null) = false StringUtils.equals("abc", "abc") = true StringUtils.equals("abc", "ABC") = false- Parameters:
cs1- the first String, may benullcs2- the second String, may benull- Returns:
trueif the Strings are equal (case-sensitive), or bothnull- See Also:
-
substring
Gets a substring from the specified String avoiding exceptions.
A negative start position can be used to start
ncharacters from the end of the String.A
nullString will returnnull. An empty ("") String will return "".StringUtils.substring(null, *) = null StringUtils.substring("", *) = "" StringUtils.substring("abc", 0) = "abc" StringUtils.substring("abc", 2) = "c" StringUtils.substring("abc", 4) = "" StringUtils.substring("abc", -2) = "bc" StringUtils.substring("abc", -4) = "abc"- Parameters:
str- the String to get the substring from, may be nullstart- the position to start from, negative means count back from the end of the String by this many characters- Returns:
- substring from start position,
nullif null String input - See Also:
-
substring
Gets a substring from the specified String avoiding exceptions.
A negative start position can be used to start/end
ncharacters from the end of the String.The returned substring starts with the character in the
startposition and ends before theendposition. All position counting is zero-based -- i.e., to start at the beginning of the string usestart = 0. Negative start and end positions can be used to specify offsets relative to the end of the String.If
startis not strictly to the left ofend, "" is returned.StringUtils.substring(null, *, *) = null StringUtils.substring("", * , *) = ""; StringUtils.substring("abc", 0, 2) = "ab" StringUtils.substring("abc", 2, 0) = "" StringUtils.substring("abc", 2, 4) = "c" StringUtils.substring("abc", 4, 6) = "" StringUtils.substring("abc", 2, 2) = "" StringUtils.substring("abc", -2, -1) = "b" StringUtils.substring("abc", -4, 2) = "ab"- Parameters:
str- the String to get the substring from, may be nullstart- the position to start from, negative means count back from the end of the String by this many charactersend- the position to end at (exclusive), negative means count back from the end of the String by this many scharacters- Returns:
- substring from start position to end position,
nullif null String input - See Also:
-
upperCase
Converts a String to upper case as per
String.toUpperCase().A
nullinput String returnsnull.StringUtils.upperCase(null) = null StringUtils.upperCase("") = "" StringUtils.upperCase("aBc") = "ABC"This uses "ENGLISH" as the locale.
- Parameters:
str- the String to upper case, may be null- Returns:
- the upper cased String,
nullif null String input - See Also:
-
lowerCase
Converts a String to lower case as per
String.toLowerCase().A
nullinput String returnsnull.StringUtils.lowerCase(null) = null StringUtils.lowerCase("") = "" StringUtils.lowerCase("aBc") = "abc"This uses "ENGLISH" as the locale.
- Parameters:
str- the String to lower case, may be null- Returns:
- the lower cased String,
nullif null String input - See Also:
-
capitalize
Capitalizes a String changing the first character to title case as per
Character.toTitleCase(int). No other characters are changed.StringUtils.capitalize(null) = null StringUtils.capitalize("") = "" StringUtils.capitalize("cat") = "Cat" StringUtils.capitalize("cAt") = "CAt" StringUtils.capitalize("'cat'") = "'cat'"- Parameters:
str- the String to capitalize, may be null- Returns:
- the capitalized String,
nullif null String input - Since:
- 2.0
- See Also:
-
uncapitalize
Uncapitalizes a String, changing the first character to lower case as per
Character.toLowerCase(int). No other characters are changed.StringUtils.uncapitalize(null) = null StringUtils.uncapitalize("") = "" StringUtils.uncapitalize("cat") = "cat" StringUtils.uncapitalize("Cat") = "cat" StringUtils.uncapitalize("CAT") = "cAT"- Parameters:
str- the String to uncapitalize, may be null- Returns:
- the uncapitalized String,
nullif null String input - Since:
- 2.0
- See Also:
-
startsWithIgnoreCase
Tests if this string starts with the specified prefix ignoring case considerations.- Parameters:
str- the string to be testedprefix- the prefix- Returns:
- true if the string starts with the prefix ignoring case
-
repeat
Repeat a String
repeattimes to form a new String.StringUtils.repeat(null, 2) = null StringUtils.repeat("", 0) = "" StringUtils.repeat("", 2) = "" StringUtils.repeat("a", 3) = "aaa" StringUtils.repeat("ab", 2) = "abab" StringUtils.repeat("a", -2) = ""- Parameters:
str- the String to repeat, may be nullrepeat- number of times to repeat str, negative treated as zero- Returns:
- a new String consisting of the original String repeated,
nullif null String input - See Also:
-
repeat
Returns padding using the specified delimiter repeated to a given length.
StringUtils.repeat('e', 0) = "" StringUtils.repeat('e', 3) = "eee" StringUtils.repeat('e', -2) = ""Note: this method does not support padding with Unicode Supplementary Characters as they require a pair of
chars to be represented. If you are needing to support full I18N of your applications consider usingrepeat(String, int)instead.- Parameters:
ch- character to repeatrepeat- number of times to repeat char, negative treated as zero- Returns:
- String with repeated character
- See Also:
-
rightPad
Right pad a String with spaces (' ').
The String is padded to the size of
size.StringUtils.rightPad(null, *) = null StringUtils.rightPad("", 3) = " " StringUtils.rightPad("bat", 3) = "bat" StringUtils.rightPad("bat", 5) = "bat " StringUtils.rightPad("bat", 1) = "bat" StringUtils.rightPad("bat", -1) = "bat"- Parameters:
str- the String to pad out, may be nullsize- the size to pad to- Returns:
- right padded String or original String if no padding is necessary,
nullif null String input - See Also:
-
rightPad
Right pad a String with a specified character.
The String is padded to the size of
size.StringUtils.rightPad(null, *, *) = null StringUtils.rightPad("", 3, 'z') = "zzz" StringUtils.rightPad("bat", 3, 'z') = "bat" StringUtils.rightPad("bat", 5, 'z') = "batzz" StringUtils.rightPad("bat", 1, 'z') = "bat" StringUtils.rightPad("bat", -1, 'z') = "bat"- Parameters:
str- the String to pad out, may be nullsize- the size to pad topadChar- the character to pad with- Returns:
- right padded String or original String if no padding is necessary,
nullif null String input - Since:
- 2.0
- See Also:
-
rightPad
Right pad a String with a specified String.
The String is padded to the size of
size.StringUtils.rightPad(null, *, *) = null StringUtils.rightPad("", 3, "z") = "zzz" StringUtils.rightPad("bat", 3, "yz") = "bat" StringUtils.rightPad("bat", 5, "yz") = "batyz" StringUtils.rightPad("bat", 8, "yz") = "batyzyzy" StringUtils.rightPad("bat", 1, "yz") = "bat" StringUtils.rightPad("bat", -1, "yz") = "bat" StringUtils.rightPad("bat", 5, null) = "bat " StringUtils.rightPad("bat", 5, "") = "bat "- Parameters:
str- the String to pad out, may be nullsize- the size to pad topadStr- the String to pad with, null or empty treated as single space- Returns:
- right padded String or original String if no padding is necessary,
nullif null String input - See Also:
-
leftPad
Left pad a String with spaces (' ').
The String is padded to the size of
size.StringUtils.leftPad(null, *) = null StringUtils.leftPad("", 3) = " " StringUtils.leftPad("bat", 3) = "bat" StringUtils.leftPad("bat", 5) = " bat" StringUtils.leftPad("bat", 1) = "bat" StringUtils.leftPad("bat", -1) = "bat"- Parameters:
str- the String to pad out, may be nullsize- the size to pad to- Returns:
- left padded String or original String if no padding is necessary,
nullif null String input - See Also:
-
leftPad
Left pad a String with a specified character.
Pad to a size of
size.StringUtils.leftPad(null, *, *) = null StringUtils.leftPad("", 3, 'z') = "zzz" StringUtils.leftPad("bat", 3, 'z') = "bat" StringUtils.leftPad("bat", 5, 'z') = "zzbat" StringUtils.leftPad("bat", 1, 'z') = "bat" StringUtils.leftPad("bat", -1, 'z') = "bat"- Parameters:
str- the String to pad out, may be nullsize- the size to pad topadChar- the character to pad with- Returns:
- left padded String or original String if no padding is necessary,
nullif null String input - Since:
- 2.0
- See Also:
-
leftPad
Left pad a String with a specified String.
Pad to a size of
size.StringUtils.leftPad(null, *, *) = null StringUtils.leftPad("", 3, "z") = "zzz" StringUtils.leftPad("bat", 3, "yz") = "bat" StringUtils.leftPad("bat", 5, "yz") = "yzbat" StringUtils.leftPad("bat", 8, "yz") = "yzyzybat" StringUtils.leftPad("bat", 1, "yz") = "bat" StringUtils.leftPad("bat", -1, "yz") = "bat" StringUtils.leftPad("bat", 5, null) = " bat" StringUtils.leftPad("bat", 5, "") = " bat"- Parameters:
str- the String to pad out, may be nullsize- the size to pad topadStr- the String to pad with, null or empty treated as single space- Returns:
- left padded String or original String if no padding is necessary,
nullif null String input - See Also:
-
strip
Strips any of a set of characters from the start and end of a String. This is similar to
String.trim()but allows the characters to be stripped to be controlled.A
nullinput String returnsnull. An empty string ("") input returns the empty string.If the stripChars String is
null, whitespace is stripped as defined byCharacter.isWhitespace(char).StringUtils.strip(null, *) = null StringUtils.strip("", *) = "" StringUtils.strip("abc", null) = "abc" StringUtils.strip(" abc", null) = "abc" StringUtils.strip("abc ", null) = "abc" StringUtils.strip(" abc ", null) = "abc" StringUtils.strip(" abcyx", "xyz") = " abc"- Parameters:
str- the String to remove characters from, may be nullstripChars- the characters to remove, null treated as whitespace- Returns:
- the stripped String,
nullif null String input - See Also:
-
stripEnd
Strips any of a set of characters from the end of a String.
A
nullinput String returnsnull. An empty string ("") input returns the empty string.If the stripChars String is
null, whitespace is stripped as defined byCharacter.isWhitespace(char).StringUtils.stripEnd(null, *) = null StringUtils.stripEnd("", *) = "" StringUtils.stripEnd("abc", "") = "abc" StringUtils.stripEnd("abc", null) = "abc" StringUtils.stripEnd(" abc", null) = " abc" StringUtils.stripEnd("abc ", null) = "abc" StringUtils.stripEnd(" abc ", null) = " abc" StringUtils.stripEnd(" abcyx", "xyz") = " abc" StringUtils.stripEnd("120.00", ".0") = "12"- Parameters:
str- the String to remove characters from, may be nullstripChars- the set of characters to remove, null treated as whitespace- Returns:
- the stripped String,
nullif null String input - See Also:
-
stripStart
Strips any of a set of characters from the start of a String.
A
nullinput String returnsnull. An empty string ("") input returns the empty string.If the stripChars String is
null, whitespace is stripped as defined byCharacter.isWhitespace(char).StringUtils.stripStart(null, *) = null StringUtils.stripStart("", *) = "" StringUtils.stripStart("abc", "") = "abc" StringUtils.stripStart("abc", null) = "abc" StringUtils.stripStart(" abc", null) = "abc" StringUtils.stripStart("abc ", null) = "abc " StringUtils.stripStart(" abc ", null) = "abc " StringUtils.stripStart("yxabc ", "xyz") = "abc "- Parameters:
str- the String to remove characters from, may be nullstripChars- the characters to remove, null treated as whitespace- Returns:
- the stripped String,
nullif null String input - See Also:
-
stripToEmpty
Strips whitespace from the start and end of a String returning an empty String if
nullinput.This is similar to
trimToEmpty(String)but removes whitespace. Whitespace is defined byCharacter.isWhitespace(char).StringUtils.stripToEmpty(null) = "" StringUtils.stripToEmpty("") = "" StringUtils.stripToEmpty(" ") = "" StringUtils.stripToEmpty("abc") = "abc" StringUtils.stripToEmpty(" abc") = "abc" StringUtils.stripToEmpty("abc ") = "abc" StringUtils.stripToEmpty(" abc ") = "abc" StringUtils.stripToEmpty(" ab c ") = "ab c"- Parameters:
str- the String to be stripped, may be null- Returns:
- the trimmed String, or an empty String if
nullinput - Since:
- 2.0
- See Also:
-
wrap
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.
(assuming that '\n' is the systems line separator)Examples 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 - http://commons.apache.org" 20 "Click here to jump\nto the commons\nwebsite -\nhttp://commons.apache.org" "Click here, http://commons.apache.org, to jump to the commons website" 20 "Click here,\nhttp://commons.apache.org,\nto jump to the\ncommons website" - Parameters:
str- the String to be word wrapped, may be nullwrapLength- the column to wrap the words at, less than 1 is treated as 1- Returns:
- a line with newlines inserted,
nullif null input - See Also:
-
wrap
Wraps a single line of text, identifying words by
' '.Leading spaces on a new line are stripped. Trailing spaces are not stripped.
Examples 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 - http://commons.apache.org" 20 "\n" false "Click here to jump\nto the commons\nwebsite -\nhttp://commons.apache.org" "Click here to jump to the commons website - http://commons.apache.org" 20 "\n" true "Click here to jump\nto the commons\nwebsite -\nhttp://commons.apach\ne.org" - Parameters:
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,nulluses the system property line separatorwrapLongWords- true if long words (such as URLs) should be wrapped- Returns:
- a line with newlines inserted,
nullif null input - See Also:
-
wrap
public static String wrap(String str, int wrapLength, String newLineStr, boolean wrapLongWords, String wrapOn) Wraps a single line of text, identifying words by
wrapOn.Leading spaces on a new line are stripped. Trailing spaces are not stripped.
Examples 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 - http://commons.apache.org" 20 "\n" false " " "Click here to jump\nto the commons\nwebsite -\nhttp://commons.apache.org" "Click here to jump to the commons website - http://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" - Parameters:
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,nulluses 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 used- Returns:
- a line with newlines inserted,
nullif null input - See Also:
-
escapeJavaString
-
levenshteinDistance
Find the Levenshtein distance between two CharSequences if it's less than or equal to a given threshold.This code is a copy of the
limitedComparemethod from Apache commons-text LevenshteinDistance.java.This implementation follows from Algorithms on Strings, Trees and Sequences by Dan Gusfield and Chas Emerick's implementation of the Levenshtein distance algorithm from http://www.merriampark.com/ld.htm
limitedCompare(null, *, *) = IllegalArgumentException limitedCompare(*, null, *) = IllegalArgumentException limitedCompare(*, *, -1) = IllegalArgumentException limitedCompare("","", 0) = 0 limitedCompare("aaapppp", "", 8) = 7 limitedCompare("aaapppp", "", 7) = 7 limitedCompare("aaapppp", "", 6)) = -1 limitedCompare("elephant", "hippo", 7) = 7 limitedCompare("elephant", "hippo", 6) = -1 limitedCompare("hippo", "elephant", 7) = 7 limitedCompare("hippo", "elephant", 6) = -1- Parameters:
left- the first CharSequence, must not be nullright- the second CharSequence, must not be nullthreshold- the target threshold, must not be negative- Returns:
- result distance, or -1
- See Also:
-
indent
Indents each line in the provided string by the provided number of spaces.- Parameters:
string- the string to indent.spaces- the number of spaces.- Returns:
- the indented string.
-