public final class StringUtils extends Object
| Modifier and Type | Method and Description |
|---|---|
static boolean |
equalsIgnoreCase(String str1,
String str2)
Compares two Strings, returning
true if they are equal ignoring
the case. |
static String |
escape(char c)
Replaces carriage returns, newlines, tabs, formfeeds and the special chars defined in
Characters
with their respective escape sequences. |
static String |
escape(String string)
Replaces carriage returns, newlines, tabs, formfeeds and the special chars defined in
Characters
with their respective escape sequences. |
static boolean |
isEmpty(String str)
Checks if a String is empty ("") or null.
|
static boolean |
isNotEmpty(String str)
Checks if a String is not empty ("") and not null.
|
static String |
join(Iterable iterable,
String separator)
Joins the elements of the provided
Iterable into
a single String containing the provided elements. |
static String |
join(Iterator iterator,
String separator)
Joins the elements of the provided
Iterator into
a single String containing the provided elements. |
static String |
join(Object[] array,
String separator)
Joins the elements of the provided array into a single String
containing the provided list of elements.
|
static String |
join(Object[] array,
String separator,
int startIndex,
int endIndex)
Joins the elements of the provided array into a single String
containing the provided list of elements.
|
static String |
left(String str,
int len)
Gets the leftmost
len characters of a String. |
static int |
length(String str)
Gets a String's length or
0 if the String is null. |
static String |
mid(String str,
int pos,
int len)
Gets
len characters from the middle of a String. |
static String |
repeat(char c,
int n)
Creates a string consisting of n times the given character.
|
static String |
right(String str,
int len)
Gets the rightmost
len characters of a String. |
static boolean |
startsWith(String string,
String prefix)
Test whether a string starts with a given prefix, handling null values without exceptions.
|
static String |
substring(String str,
int start)
Gets a substring from the specified String avoiding exceptions.
|
static String |
substring(String str,
int start,
int end)
Gets a substring from the specified String avoiding exceptions.
|
public static String escape(String string)
Characters
with their respective escape sequences.string - the stringpublic static String escape(char c)
Characters
with their respective escape sequences.c - the character to escapepublic static String repeat(char c, int n)
c - the charn - the number of times to repeatpublic static String join(Iterable iterable, String separator)
Joins the elements of the provided Iterable into
a single String containing the provided elements.
No delimiter is added before or after the list.
A null separator is the same as an empty String ("").
iterable - the Iterable of values to join together, may be nullseparator - the separator character to use, null treated as ""null if null iterator inputpublic static String join(Iterator iterator, String separator)
Joins the elements of the provided Iterator into
a single String containing the provided elements.
No delimiter is added before or after the list.
A null separator is the same as an empty String ("").
iterator - the Iterator of values to join together, may be nullseparator - the separator character to use, null treated as ""null if null iterator inputpublic static String join(Object[] array, String separator)
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 null separator 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"
array - the array of values to join together, may be nullseparator - the separator character to use, null treated as ""null if null array inputpublic static String join(Object[] array, String separator, int startIndex, int endIndex)
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 null separator 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"
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 arraynull if null array inputpublic static boolean isEmpty(String str)
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
str - the String to check, may be nulltrue if the String is empty or nullpublic static boolean isNotEmpty(String str)
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
str - the String to check, may be nulltrue if the String is not empty and not nullpublic static int length(String str)
0 if the String is null.str - a String or null0 if the String is null.public static boolean equalsIgnoreCase(String str1, String str2)
Compares two Strings, returning true if they are equal ignoring
the case.
nulls are handled without exceptions. Two null
references 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
str1 - the first String, may be nullstr2 - the second String, may be nulltrue if the Strings are equal, case insensitive, or
both nullpublic static boolean startsWith(String string, String prefix)
string - the stringprefix - the prefixpublic static String substring(String str, int start)
Gets a substring from the specified String avoiding exceptions.
A negative start position can be used to start n
characters from the end of the String.
A null String will return null.
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"
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 charactersnull if null String inputpublic static String substring(String str, int start, int end)
Gets a substring from the specified String avoiding exceptions.
A negative start position can be used to start/end n
characters from the end of the String.
The returned substring starts with the character in the start
position and ends before the end position. All position counting is
zero-based -- i.e., to start at the beginning of the string use
start = 0. Negative start and end positions can be used to
specify offsets relative to the end of the String.
If start is not strictly to the left of end, ""
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"
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 charactersnull if null String inputpublic static String left(String str, int len)
Gets the leftmost len characters of a String.
If len characters are not available, or the
String is null, 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"
str - the String to get the leftmost characters from, may be nulllen - the length of the required String, must be zero or positivenull if null String inputpublic static String right(String str, int len)
Gets the rightmost len characters of a String.
If len characters are not available, or the String
is null, 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"
str - the String to get the rightmost characters from, may be nulllen - the length of the required String, must be zero or positivenull if null String inputpublic static String mid(String str, int pos, int len)
Gets len characters from the middle of a String.
If len characters are not available, the remainder
of the String will be returned without an exception. If the
String is null, null will 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"
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 positivenull if null String inputCopyright © 2019. All rights reserved.