public final class StringTools
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
static class |
StringTools.NullableStringComparator
A comparator for strings including
null. |
| Modifier and Type | Method and Description |
|---|---|
static java.lang.String |
defaultIfEmpty(java.lang.String s,
java.lang.String defaultValue)
Returns
s if it has value (not null and not the empty string),
else returns defaultValue. |
static java.lang.String |
emptyToNull(java.lang.String string)
Returns null instead of the empty string; otherwise the given string is
returned.
|
static boolean |
equal(java.lang.String str1,
java.lang.Object str2)
Returns whether two strings are equal.
|
static java.lang.String |
fromByteArray(byte[] bytes,
java.lang.String encoding)
Decodes the given byte array to a string using the specified encoding.
|
static boolean |
hasValue(java.lang.String s)
Returns true, if the given String
s actually has a value set,
ie. it is not null and not an empty String. |
static java.lang.String |
intern(java.lang.String s)
Interns the designated string if we it is not
null. |
static java.lang.String |
join(java.lang.String glue,
java.util.Collection<java.lang.String> tokens)
Does the opposite of
String.split(String). |
static java.lang.String |
join(java.lang.String glue,
java.lang.Object... tokens)
Does the opposite of
String.split(String). |
static java.lang.String |
join(java.lang.String glue,
java.lang.String... tokens)
Does the opposite of
String.split(String). |
static java.lang.String[] |
join(java.lang.String glue,
java.lang.String[]... tokens)
This method is equal to
join(String, String...) except that it
works on arrays of strings and not individual strings. |
static java.lang.String |
lpad(java.lang.String string,
int length)
Returns the given string padded with spaces on the left up the given
length.
|
static java.lang.String |
lpad(java.lang.String string,
int length,
java.lang.String padString)
Returns the given string padded with characters from the specified pad
string on the left up the given length.
|
static java.lang.String |
ltrim(java.lang.String string)
Removes all whitespace (as defined in
Character.isWhitespace(char))
from the left side of the given string. |
static java.lang.String |
ltrim(java.lang.String string,
java.lang.String trimChars)
Removes all characters defined in
trimChars from the left side of
the given string. |
static java.lang.String |
nullToEmpty(java.lang.String string)
Returns the empty string instead of null; otherwise the given string is
returned.
|
static java.lang.String |
pad(java.lang.String string,
int length)
Returns the given string padded with spaces equally on both sides up the
given length.
|
static java.lang.String |
pad(java.lang.String string,
int length,
java.lang.String padString)
Returns the given string padded with characters from the specified pad
string equally on both sides up the given length.
|
static java.util.List<java.lang.String> |
prefix(java.lang.String prefix,
java.util.List<java.lang.String> strings)
Prepends the given prefix to each string and returns them in a new list.
|
static java.lang.String[] |
prefix(java.lang.String prefix,
java.lang.String... strings)
Prepends the given prefix to each string and returns them in a new array.
|
static java.lang.String |
repeat(java.lang.String string,
int times)
Returns a string representing the specified string repeated the specified
number of times.
|
static java.lang.String |
repeat(java.lang.String string,
java.lang.String glue,
int times)
Returns a string representing the specified string repeated the specified
number of times.
|
static java.lang.String |
replaceTabsAndNewlines(java.lang.String string,
java.lang.String replacement)
Replace all tab and new line characters (\t, \r, \n, \r\n) from the
designated string with the given replacement.
|
static java.lang.String |
rpad(java.lang.String string,
int length)
Returns the given string padded with spaces on the right up the given
length.
|
static java.lang.String |
rpad(java.lang.String string,
int length,
java.lang.String padString)
Returns the given string padded with characters from the specified pad
string on the right up the given length.
|
static java.lang.String |
rtrim(java.lang.String string)
Removes all whitespace (as defined in
Character.isWhitespace(char))
from the right side of the given string. |
static java.lang.String |
rtrim(java.lang.String string,
java.lang.String trimChars)
Removes all characters defined in
trimChars from the right side of
the given string. |
static void |
splitClasspath(java.util.List<java.lang.String> classPath)
Splits each element of the designated list at ";".
|
static java.util.List<java.lang.String> |
suffix(java.lang.String suffix,
java.util.List<java.lang.String> strings)
Appends the given suffix to each string and returns them in a new list.
|
static java.lang.String[] |
suffix(java.lang.String suffix,
java.lang.String... strings)
Appends the given suffix to each string and returns them in a new array.
|
static java.util.List<java.lang.String> |
surround(java.lang.String prefix,
java.lang.String suffix,
java.util.List<java.lang.String> strings)
Surrounds the given strings with the specified prefix and suffix and
returns them in a new list.
|
static java.lang.String[] |
surround(java.lang.String prefix,
java.lang.String suffix,
java.lang.String... strings)
Surrounds the given strings with the specified prefix and suffix and
returns them in a new array.
|
static byte[] |
toByteArray(java.lang.String string,
java.lang.String encoding)
Encodes the given string to a byte array using the specified encoding.
|
static java.lang.String[] |
toLower(java.lang.String[] strings)
Converts all strings in the given array to lower case and returns them in a
new array.
|
static java.lang.String[] |
toUpper(java.lang.String[] strings)
Converts all strings in the given array to upper case and returns them in a
new array.
|
static java.lang.String |
trim(java.lang.String string)
Removes all whitespace (as defined in
Character.isWhitespace(char))
from both sides of the given string. |
static java.lang.String |
trim(java.lang.String string,
java.lang.String trimChars)
Removes all characters defined in
trimChars from both sides of the
given string. |
static java.lang.String |
trimToNull(java.lang.String string)
Trims the designated string and if it is empty afterwards,
null will be returned. |
static java.lang.String |
trunc(java.lang.String string,
int length)
Truncate the string on his right side to the given length if necessary.
|
public static java.lang.String nullToEmpty(java.lang.String string)
string - a string or nullpublic static java.lang.String emptyToNull(java.lang.String string)
string - a string or nullpublic static java.lang.String trimToNull(java.lang.String string)
null will be returned. This will also apply if the designated
string is null.string - The string which to trim.null if the string is
null or empty after trimming.public static boolean equal(java.lang.String str1,
java.lang.Object str2)
Analogous to String.equals(Object) the right-hand parameter may be
any object
str1 - a string or nullstr2 - another string or null; analogous to
String.equals(Object) the right-hand parameter may
actually be any objectnullpublic static java.lang.String join(java.lang.String glue,
java.lang.Object... tokens)
String.split(String).glue - the string to put in between each pair of joined tokenstokens - the strings to be joinedpublic static java.lang.String join(java.lang.String glue,
java.lang.String... tokens)
String.split(String).glue - the string to put in between each pair of joined tokenstokens - the strings to be joinedpublic static java.lang.String join(java.lang.String glue,
java.util.Collection<java.lang.String> tokens)
String.split(String).glue - the string to put in between each pair of joined tokenstokens - the strings to be joinedpublic static java.lang.String[] join(java.lang.String glue,
java.lang.String[]... tokens)
join(String, String...) except that it
works on arrays of strings and not individual strings. So the first string
of tokens[0] is joined with the first strings of tokens[1],
tokens[2], ...; the second string of tokens[0] is joined
with the second strings of tokens[1], tokens[2], ...; etc.;glue - the string to put in between each pair of joined tokenstokens - the arrays of tokens to be joinedpublic static java.lang.String[] prefix(java.lang.String prefix,
java.lang.String... strings)
strings - the strings to be prefixedprefix - the prefixpublic static java.util.List<java.lang.String> prefix(java.lang.String prefix,
java.util.List<java.lang.String> strings)
strings - the strings to be prefixedprefix - the prefixpublic static java.lang.String[] suffix(java.lang.String suffix,
java.lang.String... strings)
strings - the strings to be suffixedsuffix - the suffixpublic static java.util.List<java.lang.String> suffix(java.lang.String suffix,
java.util.List<java.lang.String> strings)
strings - the strings to be suffixedsuffix - the suffixpublic static java.lang.String[] surround(java.lang.String prefix,
java.lang.String suffix,
java.lang.String... strings)
strings - the strings to be suffixedprefix - the prefixsuffix - the suffixpublic static java.util.List<java.lang.String> surround(java.lang.String prefix,
java.lang.String suffix,
java.util.List<java.lang.String> strings)
strings - the strings to be suffixedprefix - the prefixsuffix - the suffixpublic static java.lang.String repeat(java.lang.String string,
int times)
string - The string to be repeated (must not be null).times - the times the string should be repeatedpublic static java.lang.String repeat(java.lang.String string,
java.lang.String glue,
int times)
string - The string to be repeated (must not be null).glue - the string to appear between each repetitiontimes - the times the string should be repeatedpublic static java.lang.String pad(java.lang.String string,
int length)
string - The string to be padded (may be null).length - the desired length after paddingnull if the designated
string is null.public static java.lang.String lpad(java.lang.String string,
int length)
string - The string to be padded (may be null).length - the desired length after paddingnull if the designated string is
null.public static java.lang.String rpad(java.lang.String string,
int length)
string - The string to be padded (may be null).length - the desired length after paddingnull if the designated string is
null.public static java.lang.String pad(java.lang.String string,
int length,
java.lang.String padString)
string - The string to be padded (may be null).length - the desired length after paddingpadString - the string containing the characters used for paddingnull if the designated string is null.public static java.lang.String lpad(java.lang.String string,
int length,
java.lang.String padString)
string - The string to be padded (may be null).length - the desired length after paddingpadString - the string containing the characters used for paddingnull if the designated string is null.public static java.lang.String rpad(java.lang.String string,
int length,
java.lang.String padString)
string - The string to be padded (may be null).length - the desired length after paddingpadString - the string containing the characters used for paddingnull if the designated string is null.public static java.lang.String replaceTabsAndNewlines(java.lang.String string,
java.lang.String replacement)
string - The string to be changed (may be null).replacement - The replacement that should be used for any tab and
newline character.null if the designated string is
null.public static java.lang.String trim(java.lang.String string)
Character.isWhitespace(char))
from both sides of the given string.string - The string to be trimmed (may be null).null if the designated string is
null.public static java.lang.String ltrim(java.lang.String string)
Character.isWhitespace(char))
from the left side of the given string.string - The string to be trimmed (may be null).null if the designated string is
null.public static java.lang.String rtrim(java.lang.String string)
Character.isWhitespace(char))
from the right side of the given string.string - The string to be trimmed (may be null).null if the designated string is
null.public static java.lang.String trim(java.lang.String string,
java.lang.String trimChars)
trimChars from both sides of the
given string.string - The string to be trimmed (may be null).trimChars - the chars to be trimmed away from both sides of the stringnull if the designated string is
null.public static java.lang.String ltrim(java.lang.String string,
java.lang.String trimChars)
trimChars from the left side of
the given string.string - The string to be trimmed (may be null)trimChars - the chars to be trimmed away from the left side of the
stringnull if the designated string is
null.public static java.lang.String rtrim(java.lang.String string,
java.lang.String trimChars)
trimChars from the right side of
the given string.string - The string to be trimmed (may be null).trimChars - the chars to be trimmed away from the right side of the
stringnull if the designated string
is null.public static java.lang.String trunc(java.lang.String string,
int length)
string - The string to be truncated (may be null).length - the desired length of the stringnull if the designated string
is null.public static java.lang.String[] toLower(java.lang.String[] strings)
strings - String.toLowerCase()public static java.lang.String[] toUpper(java.lang.String[] strings)
strings - String.toUpperCase()public static byte[] toByteArray(java.lang.String string,
java.lang.String encoding)
throws java.io.UnsupportedEncodingException
string - the string to be encodedencoding - the encoding to be usedjava.io.UnsupportedEncodingException - if the specified encoding is not
supportedpublic static java.lang.String fromByteArray(byte[] bytes,
java.lang.String encoding)
throws java.io.UnsupportedEncodingException
bytes - the byte array to be decodedencoding - the encoding to be usedjava.io.UnsupportedEncodingException - if the specified encoding is not
supportedpublic static void splitClasspath(java.util.List<java.lang.String> classPath)
classPath - The classpath as list to separate not yet separated
classpaths.public static boolean hasValue(java.lang.String s)
s actually has a value set,
ie. it is not null and not an empty String.s - The string to be checked.public static java.lang.String defaultIfEmpty(java.lang.String s,
java.lang.String defaultValue)
s if it has value (not null and not the empty string),
else returns defaultValue.s - The string to checked for value and to be used if it has a value.defaultValue - The string to be used, if s is null or
empty.s if it has a value, else defaultValuepublic static java.lang.String intern(java.lang.String s)
null.s - The string to intern or null.null will not
be interned but just returned.