Package de.aristaflow.adept2.util
Class StringTools
- java.lang.Object
-
- de.aristaflow.adept2.util.StringTools
-
public final class StringTools extends Object
This class provides some useful string-related tool methods. The focus is on the ability to write short and simple code rather than on efficiency. This should be considered when using the methods to process large amounts of strings.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
StringTools.GenericStringComparator<T>
A comparator for objects which are mapped and compared by appropriate strings.static class
StringTools.NullableStringComparator
A comparator for strings includingnull
.
-
Field Summary
Fields Modifier and Type Field Description protected static Pattern
NUMBER_SUFFIX
The pattern for the unique name number suffix.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static String
defaultIfEmpty(String s, String defaultValue)
Returnss
if it has value (not null and not the empty string), else returnsdefaultValue
.static String
emptyToNull(String string)
Returns null instead of the empty string; otherwise the given string is returned.static boolean
equal(String str1, Object str2)
Returns whether two strings are equal.static String
fromByteArray(byte[] bytes, String encoding)
Deprecated, for removal: This API element is subject to removal in a future version.UseString(byte[], String)
instead.static String
fromStream(InputStream is, Charset encoding)
Decodes the designated input stream to a string using the specified encoding.static String
getUniqueName(String name, Collection<String> allNames)
Gets the designated string as unique name with respect to the designated name collection.static String
hash(String string)
Hashes the designated string with SHA-256.static boolean
hasValue(String s)
Returns true, if the given Strings
actually has a value set, ie. it is not null and not an empty String.static String
intern(String s)
Interns the designated string if we it is notnull
.static String
join(String glue, Object... tokens)
Does the opposite ofString.split(String)
.static String
join(String glue, String... tokens)
Does the opposite ofString.split(String)
.static String[]
join(String glue, String[]... tokens)
This method is equal tojoin(String, String...)
except that it works on arrays of strings and not individual strings.static String
join(String glue, Collection<?> tokens)
Does the opposite ofString.split(String)
.static String
join(String glue, Iterator<String> tokens)
Does the opposite ofString.split(String)
.static String
lpad(String string, int length)
Returns the given string padded with spaces on the left up the given length.static String
lpad(String string, int length, String padString)
Returns the given string padded with characters from the specified pad string on the left up the given length.static String
ltrim(String string)
Removes all whitespace (as defined inCharacter.isWhitespace(char)
) from the left side of the given string.static String
ltrim(String string, String trimChars)
Removes all characters defined intrimChars
from the left side of the given string.static String
nullToEmpty(String string)
Returns the empty string instead of null; otherwise the given string is returned.static String
pad(String string, int length)
Returns the given string padded with spaces equally on both sides up the given length.static String
pad(String string, int length, String padString)
Returns the given string padded with characters from the specified pad string equally on both sides up the given length.static String[]
prefix(String prefix, String... strings)
Prepends the given prefix to each string and returns them in a new array.static List<String>
prefix(String prefix, Collection<String> strings)
Prepends the given prefix to each string and returns them in a new list.static String
repeat(String string, int times)
Returns a string representing the specified string repeated the specified number of times.static String
repeat(String string, String glue, int times)
Returns a string representing the specified string repeated the specified number of times.static StringBuilder
replace(String del, String repl, StringBuilder sb)
Replaces the designated stringdel
by the designated stringrepl
in the designatedStringBuilder
.static String
replaceTabsAndNewlines(String string, String replacement)
Replace all tab and new line characters (\t, \r, \n, \r\n) from the designated string with the given replacement.static String
rpad(String string, int length)
Returns the given string padded with spaces on the right up the given length.static String
rpad(String string, int length, String padString)
Returns the given string padded with characters from the specified pad string on the right up the given length.static String
rtrim(String string)
Removes all whitespace (as defined inCharacter.isWhitespace(char)
) from the right side of the given string.static String
rtrim(String string, String trimChars)
Removes all characters defined intrimChars
from the right side of the given string.static String
sanitise(String evilString)
Removes bad characters from the designated string.static void
splitClasspath(List<String> classPath)
Splits each element of the designated list at ";".static String[]
suffix(String suffix, String... strings)
Appends the given suffix to each string and returns them in a new array.static List<String>
suffix(String suffix, Collection<String> strings)
Appends the given suffix to each string and returns them in a new list.static String[]
surround(String prefix, String suffix, String... strings)
Surrounds the given strings with the specified prefix and suffix and returns them in a new array.static List<String>
surround(String prefix, String suffix, List<String> strings)
Surrounds the given strings with the specified prefix and suffix and returns them in a new list.static byte[]
toByteArray(String string, String encoding)
Deprecated, for removal: This API element is subject to removal in a future version.UseString.getBytes(String)
instead.static String[]
toLower(String[] strings)
Converts all strings in the given array to lower case and returns them in a new array.static String[]
toUpper(String[] strings)
Converts all strings in the given array to upper case and returns them in a new array.static String
trim(String string)
Removes all whitespace (as defined inCharacter.isWhitespace(char)
) from both sides of the given string.static String
trim(String string, String trimChars)
Removes all characters defined intrimChars
from both sides of the given string.static String
trimToNull(String string)
Trims the designated string and if it is empty afterwards,null
will be returned.static String
trunc(String string, int length)
Truncate the string on his right side to the given length if necessary.
-
-
-
Field Detail
-
NUMBER_SUFFIX
protected static final Pattern NUMBER_SUFFIX
The pattern for the unique name number suffix.
-
-
Method Detail
-
nullToEmpty
public static String nullToEmpty(String string)
Returns the empty string instead of null; otherwise the given string is returned.- Parameters:
string
- a string ornull
- Returns:
- the empty string instead of null; otherwise the given string
-
emptyToNull
public static String emptyToNull(String string)
Returns null instead of the empty string; otherwise the given string is returned.- Parameters:
string
- a string ornull
- Returns:
- null instead of the empty string; otherwise the given string
-
trimToNull
public static String trimToNull(String string)
Trims the designated string and if it is empty afterwards,null
will be returned. This will also apply if the designated string isnull
.- Parameters:
string
- The string which to trim.- Returns:
- The trimmed string or
null
if the string isnull
or empty after trimming.
-
equal
public static boolean equal(String str1, Object str2)
Returns whether two strings are equal. This method can deal with null values which is it's real benefit.Analogous to
String.equals(Object)
the right-hand parameter may be any object- Parameters:
str1
- a string ornull
str2
- another string ornull
; analogous toString.equals(Object)
the right-hand parameter may actually be any object- Returns:
- whether both strings are equal or both are
null
-
join
public static String join(String glue, Object... tokens)
Does the opposite ofString.split(String)
.- Parameters:
glue
- the string to put in between each pair of joined tokenstokens
- the strings to be joined- Returns:
- the joined string
-
join
public static String join(String glue, String... tokens)
Does the opposite ofString.split(String)
.- Parameters:
glue
- the string to put in between each pair of joined tokenstokens
- the strings to be joined- Returns:
- the joined string
-
join
public static String join(String glue, Collection<?> tokens)
Does the opposite ofString.split(String)
.- Parameters:
glue
- the string to put in between each pair of joined tokenstokens
- the strings to be joined- Returns:
- the joined string
-
join
public static String join(String glue, Iterator<String> tokens)
Does the opposite ofString.split(String)
.- Parameters:
glue
- the string to put in between each pair of joined tokenstokens
- the strings to be joined- Returns:
- the joined string
-
join
public static String[] join(String glue, String[]... tokens)
This method is equal tojoin(String, String...)
except that it works on arrays of strings and not individual strings. So the first string oftokens[0]
is joined with the first strings oftokens[1]
,tokens[2]
, ...; the second string oftokens[0]
is joined with the second strings oftokens[1]
,tokens[2]
, ...; etc.;- Parameters:
glue
- the string to put in between each pair of joined tokenstokens
- the arrays of tokens to be joined- Returns:
- an array of the joined strings
-
prefix
public static String[] prefix(String prefix, String... strings)
Prepends the given prefix to each string and returns them in a new array.- Parameters:
strings
- the strings to be prefixedprefix
- the prefix- Returns:
- a new array containing the prefixed strings
-
prefix
public static List<String> prefix(String prefix, Collection<String> strings)
Prepends the given prefix to each string and returns them in a new list.- Parameters:
strings
- the strings to be prefixedprefix
- the prefix- Returns:
- a new list containing the prefixed strings
-
suffix
public static String[] suffix(String suffix, String... strings)
Appends the given suffix to each string and returns them in a new array.- Parameters:
strings
- the strings to be suffixedsuffix
- the suffix- Returns:
- a new array containing the suffixed strings
-
suffix
public static List<String> suffix(String suffix, Collection<String> strings)
Appends the given suffix to each string and returns them in a new list.- Parameters:
strings
- the strings to be suffixedsuffix
- the suffix- Returns:
- a new list containing the suffixed strings
-
surround
public static String[] surround(String prefix, String suffix, String... strings)
Surrounds the given strings with the specified prefix and suffix and returns them in a new array.- Parameters:
strings
- the strings to be suffixedprefix
- the prefixsuffix
- the suffix- Returns:
- a new array containing the surrounded strings
-
surround
public static List<String> surround(String prefix, String suffix, List<String> strings)
Surrounds the given strings with the specified prefix and suffix and returns them in a new list.- Parameters:
strings
- the strings to be suffixedprefix
- the prefixsuffix
- the suffix- Returns:
- a new list containing the surrounded strings
-
repeat
public static String repeat(String string, int times)
Returns a string representing the specified string repeated the specified number of times.- Parameters:
string
- The string to be repeated (must not benull
).times
- the times the string should be repeated- Returns:
- a string representing the specified string repeated the specified number of times
-
repeat
public static String repeat(String string, String glue, int times)
Returns a string representing the specified string repeated the specified number of times. Between each repetition the specified glue string is inserted.- Parameters:
string
- The string to be repeated (must not benull
).glue
- the string to appear between each repetitiontimes
- the times the string should be repeated- Returns:
- a string representing the specified string repeated the specified number of times
-
pad
public static String pad(String string, int length)
Returns the given string padded with spaces equally on both sides up the given length. If the given length is negative or the string already longer than the given length, the string itself is returned.- Parameters:
string
- The string to be padded (may benull
).length
- the desired length after padding- Returns:
- The given string padded with spaces equally on both sides up the
given length. This will be
null
if the designated string isnull
.
-
lpad
public static String lpad(String string, int length)
Returns the given string padded with spaces on the left up the given length. If the given length is negative or the string already longer than the given length, the string itself is returned.- Parameters:
string
- The string to be padded (may benull
).length
- the desired length after padding- Returns:
- The given string padded with spaces on the left up the given
length. This will be
null
if the designated string isnull
.
-
rpad
public static String rpad(String string, int length)
Returns the given string padded with spaces on the right up the given length. If the given length is negative or the string already longer than the given length, the string itself is returned.- Parameters:
string
- The string to be padded (may benull
).length
- the desired length after padding- Returns:
- The given string padded with spaces on the right up the given
length. This will be
null
if the designated string isnull
.
-
pad
public static String pad(String string, int length, String padString)
Returns the given string padded with characters from the specified pad string equally on both sides up the given length. If the given length is negative or the string already longer than the given length, the string itself is returned.- Parameters:
string
- The string to be padded (may benull
).length
- the desired length after paddingpadString
- the string containing the characters used for padding- Returns:
- The given string padded with characters from the specified pad
string equally on both sides up the given length. This will be
null
if the designated string isnull
.
-
lpad
public static String lpad(String string, int length, String padString)
Returns the given string padded with characters from the specified pad string on the left up the given length. If the given length is negative or the string already longer than the given length, the string itself is returned.- Parameters:
string
- The string to be padded (may benull
).length
- the desired length after paddingpadString
- the string containing the characters used for padding- Returns:
- The given string padded with characters from the specified pad
string on the left up the given length. This will be
null
if the designated string isnull
.
-
rpad
public static String rpad(String string, int length, String padString)
Returns the given string padded with characters from the specified pad string on the right up the given length. If the given length is negative or the string already longer than the given length, the string itself is returned.- Parameters:
string
- The string to be padded (may benull
).length
- the desired length after paddingpadString
- the string containing the characters used for padding- Returns:
- The given string padded with characters from the specified pad
string on the right up the given length. This will be
null
if the designated string isnull
.
-
replaceTabsAndNewlines
public static String replaceTabsAndNewlines(String string, String replacement)
Replace all tab and new line characters (\t, \r, \n, \r\n) from the designated string with the given replacement.- Parameters:
string
- The string to be changed (may benull
).replacement
- The replacement that should be used for any tab and newline character.- Returns:
- The string with all tabs and newlines replaced by the replacement
string. This will be
null
if the designated string isnull
.
-
trim
public static String trim(String string)
Removes all whitespace (as defined inCharacter.isWhitespace(char)
) from both sides of the given string.- Parameters:
string
- The string to be trimmed (may benull
).- Returns:
- The trimmed string or
null
if the designated string isnull
.
-
ltrim
public static String ltrim(String string)
Removes all whitespace (as defined inCharacter.isWhitespace(char)
) from the left side of the given string.- Parameters:
string
- The string to be trimmed (may benull
).- Returns:
- The trimmed string or
null
if the designated string isnull
.
-
rtrim
public static String rtrim(String string)
Removes all whitespace (as defined inCharacter.isWhitespace(char)
) from the right side of the given string.- Parameters:
string
- The string to be trimmed (may benull
).- Returns:
- The trimmed string or
null
if the designated string isnull
.
-
trim
public static String trim(String string, String trimChars)
Removes all characters defined intrimChars
from both sides of the given string.- Parameters:
string
- The string to be trimmed (may benull
).trimChars
- the chars to be trimmed away from both sides of the string- Returns:
- The trimmed string or
null
if the designated string isnull
.
-
ltrim
public static String ltrim(String string, String trimChars)
Removes all characters defined intrimChars
from the left side of the given string.- Parameters:
string
- The string to be trimmed (may benull
)trimChars
- the chars to be trimmed away from the left side of the string- Returns:
- The trimmed string or
null
if the designated string isnull
.
-
rtrim
public static String rtrim(String string, String trimChars)
Removes all characters defined intrimChars
from the right side of the given string.- Parameters:
string
- The string to be trimmed (may benull
).trimChars
- the chars to be trimmed away from the right side of the string- Returns:
- The trimmed string or
null
if the designated string isnull
.
-
trunc
public static String trunc(String string, int length)
Truncate the string on his right side to the given length if necessary. Otherwise the same string is returned.- Parameters:
string
- The string to be truncated (may benull
).length
- the desired length of the string- Returns:
- The truncated string or
null
if the designated string isnull
.
-
toLower
public static String[] toLower(String[] strings)
Converts all strings in the given array to lower case and returns them in a new array.- Parameters:
strings
-- Returns:
- lower case versions of the given strings
- See Also:
String.toLowerCase()
-
toUpper
public static String[] toUpper(String[] strings)
Converts all strings in the given array to upper case and returns them in a new array.- Parameters:
strings
-- Returns:
- upper case versions of the given strings
- See Also:
String.toUpperCase()
-
toByteArray
@Deprecated(since="15.0.0", forRemoval=true) public static byte[] toByteArray(String string, String encoding) throws UnsupportedEncodingException
Deprecated, for removal: This API element is subject to removal in a future version.UseString.getBytes(String)
instead.Encodes the given string to a byte array using the specified encoding.- Parameters:
string
- the string to be encodedencoding
- the encoding to be used- Returns:
- the string encoded in a byte array using the specified encoding
- Throws:
UnsupportedEncodingException
- if the specified encoding is not supported
-
fromByteArray
@Deprecated(since="15.0.0", forRemoval=true) public static String fromByteArray(byte[] bytes, String encoding) throws UnsupportedEncodingException
Deprecated, for removal: This API element is subject to removal in a future version.UseString(byte[], String)
instead.Decodes the given byte array to a string using the specified encoding.- Parameters:
bytes
- the byte array to be decodedencoding
- the encoding to be used- Returns:
- the string decoded from the byte array using the specified encoding
- Throws:
UnsupportedEncodingException
- if the specified encoding is not supported
-
fromStream
public static String fromStream(InputStream is, Charset encoding) throws IOException
Decodes the designated input stream to a string using the specified encoding.- Parameters:
is
- The input stream to decode to a string. This method will close the stream after reading.encoding
- The encoding to be used.- Returns:
- The string decoded from the designated input stream using the specified encoding.
- Throws:
IOException
- If there are problems reading from the stream, anIOException
will be thrown.
-
splitClasspath
public static void splitClasspath(List<String> classPath)
Splits each element of the designated list at ";". This allows classpaths to be either separated by "," (the list delimiter for Apache Commons Configuration) or ";" (the normal classpath separator in AristaFlow.
The order is respected, that an element of the list having sub-elements leads to several additional list elements right after the corresponding element (depth-first).- Parameters:
classPath
- The classpath as list to separate not yet separated classpaths.
-
hasValue
public static boolean hasValue(String s)
Returns true, if the given Strings
actually has a value set, ie. it is not null and not an empty String.- Parameters:
s
- The string to be checked.- Returns:
- True if != null and != "".
-
defaultIfEmpty
public static String defaultIfEmpty(String s, String defaultValue)
Returnss
if it has value (not null and not the empty string), else returnsdefaultValue
.- Parameters:
s
- The string to checked for value and to be used if it has a value.defaultValue
- The string to be used, ifs
is null or empty.- Returns:
s
if it has a value, elsedefaultValue
-
intern
public static String intern(String s)
Interns the designated string if we it is notnull
.- Parameters:
s
- The string to intern ornull
.- Returns:
- The interned string if interning is appropriate. Apparently,
null
will not be interned but just returned.
-
replace
public static StringBuilder replace(String del, String repl, StringBuilder sb)
Replaces the designated stringdel
by the designated stringrepl
in the designatedStringBuilder
.- Parameters:
del
- The string which to replace with the other string.repl
- The string with which to replace the other string.sb
- TheStringBuilder
in which to replace the string.- Returns:
- The designated
StringBuilder
.
-
sanitise
public static String sanitise(String evilString)
Removes bad characters from the designated string. This includes "u0000" which a lot of artefacts treat as string termination (Java does not do this).- Parameters:
evilString
- The string which may contain bad characters.- Returns:
- The designated string with the bad characters replaced.
-
hash
public static String hash(String string)
Hashes the designated string with SHA-256.- Parameters:
string
- The string to be hashed.- Returns:
- The hashed string or
null
if the designated string isnull
.
-
getUniqueName
public static String getUniqueName(String name, Collection<String> allNames)
Gets the designated string as unique name with respect to the designated name collection. Uniqueness is established by appending "-1" (or another number) add the end. Understandably, the number is being incremented by one until the name is unique.- Parameters:
name
- The name which should be unique.allNames
- The names already known. If this isnull
, the name will be returned unchanged.- Returns:
- The designated name adapted so that it is unique with respect to the designated name collection.
-
-