Class StringUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanequalsIgnoreCase(String str1, String str2) Compares two Strings, returningtrueif they are equal ignoring the case.static Stringescape(char c) Replaces carriage returns, newlines, tabs, formfeeds and the special chars defined inCharacterswith their respective escape sequences.static StringReplaces carriage returns, newlines, tabs, formfeeds and the special chars defined inCharacterswith their respective escape sequences.static booleanChecks if a String is empty ("") or null.static booleanisNotEmpty(String str) Checks if a String is not empty ("") and not null.static StringJoins the elements of the providedIterableinto a single String containing the provided elements.static StringJoins the elements of the provided array into a single String containing the provided list of elements.static StringJoins the elements of the provided array into a single String containing the provided list of elements.static StringJoins the elements of the providedIteratorinto a single String containing the provided elements.static StringGets the leftmostlencharacters of a String.static intGets a String's length or0if the String isnull.static StringGetslencharacters from the middle of a String.static Stringrepeat(char c, int n) Creates a string consisting of n times the given character.static StringGets the rightmostlencharacters of a String.static booleanstartsWith(String string, String prefix) Test whether a string starts with a given prefix, handling null values without exceptions.static StringGets a substring from the specified String avoiding exceptions.static StringGets a substring from the specified String avoiding exceptions.
-
Method Details
-
escape
Replaces carriage returns, newlines, tabs, formfeeds and the special chars defined inCharacterswith their respective escape sequences.- Parameters:
string- the string- Returns:
- the escaped string
-
escape
Replaces carriage returns, newlines, tabs, formfeeds and the special chars defined inCharacterswith their respective escape sequences.- Parameters:
c- the character to escape- Returns:
- the escaped string
-
repeat
Creates a string consisting of n times the given character.- Parameters:
c- the charn- the number of times to repeat- Returns:
- the string
-
join
Joins the elements of the provided
Iterableinto a single String containing the provided elements.No delimiter is added before or after the list. A
nullseparator is the same as an empty String ("").- Parameters:
iterable- theIterableof values to join together, may be nullseparator- the separator character to use, null treated as ""- Returns:
- the joined String,
nullif null iterator input
-
join
Joins the elements of the provided
Iteratorinto a single String containing the provided elements.No delimiter is added before or after the list. A
nullseparator is the same as an empty String ("").- Parameters:
iterator- theIteratorof values to join together, may be nullseparator- the separator character to use, null treated as ""- Returns:
- the joined String,
nullif null iterator input
-
join
Joins the elements of the provided array into a single String containing the provided list of elements.
No delimiter is added before or after the list. A
nullseparator is the same as an empty String (""). Null objects or empty strings within the array are represented by empty strings.StringUtils.join(null, *) = null StringUtils.join([], *) = "" StringUtils.join([null], *) = "" StringUtils.join(["a", "b", "c"], "--") = "a--b--c" StringUtils.join(["a", "b", "c"], null) = "abc" StringUtils.join(["a", "b", "c"], "") = "abc" StringUtils.join([null, "", "a"], ',') = ",,a"
- Parameters:
array- the array of values to join together, may be nullseparator- the separator character to use, null treated as ""- Returns:
- the joined String,
nullif null array input
-
join
Joins the elements of the provided array into a single String containing the provided list of elements.
No delimiter is added before or after the list. A
nullseparator is the same as an empty String (""). Null objects or empty strings within the array are represented by empty strings.StringUtils.join(null, *) = null StringUtils.join([], *) = "" StringUtils.join([null], *) = "" StringUtils.join(["a", "b", "c"], "--") = "a--b--c" StringUtils.join(["a", "b", "c"], null) = "abc" StringUtils.join(["a", "b", "c"], "") = "abc" StringUtils.join([null, "", "a"], ',') = ",,a"
- Parameters:
array- the array of values to join together, may be nullseparator- the separator character to use, null treated as ""startIndex- the first index to start joining from. It is an error to pass in an end index past the end of the arrayendIndex- the index to stop joining from (exclusive). It is an error to pass in an end index past the end of the array- Returns:
- the joined String,
nullif null array input
-
isEmpty
Checks if a String is empty ("") or null.
StringUtils.isEmpty(null) = true StringUtils.isEmpty("") = true StringUtils.isEmpty(" ") = false StringUtils.isEmpty("bob") = false StringUtils.isEmpty(" bob ") = false- Parameters:
str- the String to check, may be null- Returns:
trueif the String is empty or null
-
isNotEmpty
Checks if a String is not empty ("") and not null.
StringUtils.isNotEmpty(null) = false StringUtils.isNotEmpty("") = false StringUtils.isNotEmpty(" ") = true StringUtils.isNotEmpty("bob") = true StringUtils.isNotEmpty(" bob ") = true- Parameters:
str- the String to check, may be null- Returns:
trueif the String is not empty and not null
-
length
Gets a String's length or0if the String isnull.- Parameters:
str- a String ornull- Returns:
- String length or
0if the String isnull.
-
equalsIgnoreCase
Compares two Strings, returning
trueif they are equal ignoring the case.nulls are handled without exceptions. Twonullreferences are considered equal. Comparison is case insensitive.StringUtils.equalsIgnoreCase(null, null) = true StringUtils.equalsIgnoreCase(null, "abc") = false StringUtils.equalsIgnoreCase("abc", null) = false StringUtils.equalsIgnoreCase("abc", "abc") = true StringUtils.equalsIgnoreCase("abc", "ABC") = true- Parameters:
str1- the first String, may be nullstr2- the second String, may be null- Returns:
trueif the Strings are equal, case insensitive, or bothnull
-
startsWith
Test whether a string starts with a given prefix, handling null values without exceptions. StringUtils.startsWith(null, null) = false StringUtils.startsWith(null, "abc") = false StringUtils.startsWith("abc", null) = true StringUtils.startsWith("abc", "ab") = true StringUtils.startsWith("abc", "abc") = true- Parameters:
string- the stringprefix- the prefix- Returns:
- true if string starts with prefix
-
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
-
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 characters- Returns:
- substring from start position to end positon,
nullif null String input
-
left
Gets the leftmost
lencharacters of a String.If
lencharacters are not available, or the String isnull, the String will be returned without an exception. An exception is thrown if len is negative.StringUtils.left(null, *) = null StringUtils.left(*, -ve) = "" StringUtils.left("", *) = "" StringUtils.left("abc", 0) = "" StringUtils.left("abc", 2) = "ab" StringUtils.left("abc", 4) = "abc"- Parameters:
str- the String to get the leftmost characters from, may be nulllen- the length of the required String, must be zero or positive- Returns:
- the leftmost characters,
nullif null String input
-
right
Gets the rightmost
lencharacters of a String.If
lencharacters are not available, or the String isnull, the String will be returned without an an exception. An exception is thrown if len is negative.StringUtils.right(null, *) = null StringUtils.right(*, -ve) = "" StringUtils.right("", *) = "" StringUtils.right("abc", 0) = "" StringUtils.right("abc", 2) = "bc" StringUtils.right("abc", 4) = "abc"- Parameters:
str- the String to get the rightmost characters from, may be nulllen- the length of the required String, must be zero or positive- Returns:
- the rightmost characters,
nullif null String input
-
mid
Gets
lencharacters from the middle of a String.If
lencharacters are not available, the remainder of the String will be returned without an exception. If the String isnull,nullwill be returned. An exception is thrown if len is negative.StringUtils.mid(null, *, *) = null StringUtils.mid(*, *, -ve) = "" StringUtils.mid("", 0, *) = "" StringUtils.mid("abc", 0, 2) = "ab" StringUtils.mid("abc", 0, 4) = "abc" StringUtils.mid("abc", 2, 4) = "c" StringUtils.mid("abc", 4, 2) = "" StringUtils.mid("abc", -2, 2) = "ab"- Parameters:
str- the String to get the characters from, may be nullpos- the position to start from, negative treated as zerolen- the length of the required String, must be zero or positive- Returns:
- the middle characters,
nullif null String input
-