Class StringUtils
- java.lang.Object
-
- org.parboiled.common.StringUtils
-
public final class StringUtils extends Object
General utility methods for string manipulation.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
equalsIgnoreCase(String str1, String str2)
Compares two Strings, returningtrue
if they are equal ignoring the case.static String
escape(char c)
Replaces carriage returns, newlines, tabs, formfeeds and the special chars defined inCharacters
with their respective escape sequences.static String
escape(String string)
Replaces carriage returns, newlines, tabs, formfeeds and the special chars defined inCharacters
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 providedIterable
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
join(Iterator iterator, String separator)
Joins the elements of the providedIterator
into a single String containing the provided elements.static String
left(String str, int len)
Gets the leftmostlen
characters of a String.static int
length(String str)
Gets a String's length or0
if the String isnull
.static String
mid(String str, int pos, int len)
Getslen
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 rightmostlen
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.
-
-
-
Method Detail
-
escape
public static String escape(String string)
Replaces carriage returns, newlines, tabs, formfeeds and the special chars defined inCharacters
with their respective escape sequences.- Parameters:
string
- the string- Returns:
- the escaped string
-
escape
public static String escape(char c)
Replaces carriage returns, newlines, tabs, formfeeds and the special chars defined inCharacters
with their respective escape sequences.- Parameters:
c
- the character to escape- Returns:
- the escaped string
-
repeat
public static String repeat(char c, int n)
Creates a string consisting of n times the given character.- Parameters:
c
- the charn
- the number of times to repeat- Returns:
- the string
-
join
public 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 ("").- Parameters:
iterable
- theIterable
of values to join together, may be nullseparator
- the separator character to use, null treated as ""- Returns:
- the joined String,
null
if null iterator input
-
join
public 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 ("").- Parameters:
iterator
- theIterator
of values to join together, may be nullseparator
- the separator character to use, null treated as ""- Returns:
- the joined String,
null
if null iterator input
-
join
public 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"
- Parameters:
array
- the array of values to join together, may be nullseparator
- the separator character to use, null treated as ""- Returns:
- the joined String,
null
if null array input
-
join
public 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"
- 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,
null
if null array input
-
isEmpty
public 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
- Parameters:
str
- the String to check, may be null- Returns:
true
if the String is empty or null
-
isNotEmpty
public 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
- Parameters:
str
- the String to check, may be null- Returns:
true
if the String is not empty and not null
-
length
public static int length(String str)
Gets a String's length or0
if the String isnull
.- Parameters:
str
- a String ornull
- Returns:
- String length or
0
if the String isnull
.
-
equalsIgnoreCase
public static boolean equalsIgnoreCase(String str1, String str2)
Compares two Strings, returning
true
if they are equal ignoring the case.null
s are handled without exceptions. Twonull
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
- Parameters:
str1
- the first String, may be nullstr2
- the second String, may be null- Returns:
true
if the Strings are equal, case insensitive, or bothnull
-
startsWith
public static boolean startsWith(String string, String prefix)
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
public 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 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,
null
if null String input
-
substring
public 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 theend
position. 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
start
is 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,
null
if null String input
-
left
public static String left(String str, int len)
Gets the leftmost
len
characters of a String.If
len
characters 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,
null
if null String input
-
right
public static String right(String str, int len)
Gets the rightmost
len
characters of a String.If
len
characters 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,
null
if null String input
-
mid
public 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 isnull
,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"
- 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,
null
if null String input
-
-