public class Strings extends Object
The Value
class provides some additional methods for working with nullable strings like
Value.left(int)
, Value.toLowerCase()
etc.
This class can and should not be instantiated, as all methods are static.
Value
Modifier and Type | Method and Description |
---|---|
static String |
apply(String format,
Object... arguments)
Formats the given pattern string format with the given arguments.
|
static boolean |
areEqual(Object left,
Object right)
Compares the given Strings just like
String.compareTo(String)
but with graceful handling for null values. |
static boolean |
equalIgnoreCase(String left,
String right)
Compares the given Strings while treating upper- and lowercase characters as equal.
|
static String |
firstFilled(String... values)
Returns the first non empty value of the given array.
|
static String |
generateCode(int length)
Generates a string of the given length, containing random character.
|
static String |
generatePassword()
Generates a random password with 7 characters length.
|
static boolean |
isEmpty(Object string)
Checks if the string representation of the given object is "" or null.
|
static boolean |
isFilled(Object string)
Checks if the string representation of the given object is neither "" nor null.
|
static String |
join(Iterable<?> list,
String separator)
Returns a string concatenation of the given lists items.
|
static String |
join(Map<?,?> source)
Returns a string representation of the given map.
|
static String |
join(String separator,
String... parts) |
static String |
leftPad(String input,
String padding,
int minLength)
Pads the given string on the left side to the given length using the given padding.
|
static String |
limit(Object input,
int length)
Limits the length of the given string to the given length.
|
static String |
limit(Object input,
int length,
boolean showEllipsis)
Limits the length of the given string to the given length.
|
static String |
reduceCharacters(String term)
Removes all umlauts and other decorated latin characters.
|
static String |
replaceAll(Pattern regEx,
String input,
Function<String,String> replacement)
Replaces all occurrences of the given regular expression by the result of the given replacement function.
|
static String |
replaceUmlautsToHtml(String input)
Replaces german umlauts to HTML entities as some email clients fail otherwise.
|
static String |
rightPad(String input,
String padding,
int minLength)
Pads the given string on the right side to the given length using the given padding.
|
static String |
shorten(String string,
int numChars)
shortens a string to the given number of chars, cutting of at most half of the string and adding ... if
something
has been cut of.
|
static Tuple<String,String> |
split(String input,
String separator)
Splits the given string at the first occurrence of the separator.
|
static Tuple<String,String> |
splitAtLast(String input,
String separator)
Splits the given string at the last occurrence of the separator.
|
static String |
toString(Object object)
Returns a string representation of the given object while gracefully handling null values.
|
static String |
trim(Object object)
Returns a trimmed version of the given object's string representation.
|
static String |
urlEncode(String value)
Returns an url encoded representation of the given value with UTF-8 as character encoding.
|
public static boolean isEmpty(@Nullable Object string)
string
- the object which is to be checkedpublic static boolean isFilled(@Nullable Object string)
string
- the object which is to be checkedpublic static boolean equalIgnoreCase(@Nullable String left, @Nullable String right)
This is essentially the same as left.equalsIgnoreCase(right)
while gracefully handling null values.
left
- the first string to be comparedright
- the second string to be compared withpublic static boolean areEqual(@Nullable Object left, @Nullable Object right)
String.compareTo(String)
but with graceful handling for null values.left
- the first string to be comparedright
- the second string to be compared with@Nullable public static String toString(@Nullable Object object)
Internally this method calls Object.toString()
. For locale aware or locale fixed methods,
NLS.toUserString(Object)
and
NLS.toMachineString(Object)
can be used.
object
- the object to be converted to string.public static String apply(String format, Object... arguments)
This is just a delegate to String.format(String, Object...)
. It is however defined in this class to
forces all framework parts to use the same formatting mechanism (and not MessageFormat etc.).
This method is intended to be used for format short strings or non-translated log messages etc. For more
complex messages and especially for translated strings, a Formatter
should be
used.
format
- the format pattern to be usedarguments
- the parameters for be used for replacementString.format(String, Object...)
,
Formatter
,
NLS.fmtr(String)
public static String firstFilled(String... values)
This can be used to provide a default value or to check several sources for a value, e.g.:
String s = Strings.firstFilled(System.getProperty("foo.test"),
System.getProperty("test"),
"default");
values
- an array of string values to be scannedpublic static String urlEncode(@Nullable String value)
value
- the value to be encoded.public static Tuple<String,String> split(String input, String separator)
If the given input is empty, a tuple with null as first and second component will be returned.
input
- the input to be splitseparator
- the separator used to split atpublic static Tuple<String,String> splitAtLast(String input, String separator)
If the given input is empty, a tuple with null as first and second component will be returned.
input
- the input to be splitseparator
- the separator used to split atpublic static String limit(@Nullable Object input, int length)
input
- the object which string representation should be limited to the given lengthlength
- the max. number of characters to returnpublic static String limit(@Nullable Object input, int length, boolean showEllipsis)
input
- the object which string representation should be limited to the given lengthlength
- the max. number of characters to returnshowEllipsis
- whether to append three dots if input is longer than length@Nonnull public static String join(@Nullable Map<?,?> source)
Keys and values are separated by a colon (:) and entries by a new line.
source
- to map to be converted to a string@Nonnull public static String join(@Nullable Iterable<?> list, @Nonnull String separator)
Generates a string which contains the string representation of each item separated by the given separator.
The conversion method for the list items used is NLS.toMachineString(Object)
. This method will filter
empty values ("" or null) and ignore those.
list
- the list items to joinseparator
- the separator to place between the itemspublic static String generatePassword()
public static String generateCode(int length)
length
- the desired length of the generated string.public static String replaceUmlautsToHtml(String input)
input
- the input to escapepublic static String reduceCharacters(String term)
term
- the term to reduce characters inpublic static String trim(Object object)
object
- the input to be converted into a string and then trimmedpublic static String shorten(String string, int numChars)
string
- string to be cut ofnumChars
- new maximum length of stringpublic static String replaceAll(Pattern regEx, String input, Function<String,String> replacement)
The regular expression is expected to have one explicit matching group which will be used as input for the replacement function.
To replace all occurrences of #{X}
by NLS.get("X")
one could use:
Strings.replaceAll(Pattern.compile("#\\{([^\\}]+)\\
"), someText, NLS::get)}
regEx
- the regular expression to replace in the given inputinput
- the input to scanreplacement
- the replacement function which transforms the first group of the match into the string used
as replacement for the whole match.public static String leftPad(String input, String padding, int minLength)
Note that if padding consists of several characters, the final string might be longer than minLength as no substring but only the full value of padding is used to pad. Implementation detail: This method checks if padding is necessary at all. If not, it directly returns the given input. This should enable inlining and therefore create a fast path if no padding is necessary.
input
- the input to padpadding
- the padding to useminLength
- the minimal length to reachpublic static String rightPad(String input, String padding, int minLength)
Note that if padding consists of several characters, the final string might be longer than minLength as no substring but only the full value of padding is used to pad. Implementation detail: This method checks if padding is necessary at all. If not, it directly returns the given input. This should enable inlining and therefore create a fast path if no padding is necessary.
input
- the input to padpadding
- the padding to useminLength
- the minimal length to reachCopyright © 2018. All rights reserved.