@GwtCompatible public final class Strings extends Object
String or
CharSequence instances.
| Modifier and Type | Method and Description |
|---|---|
static String |
commonPrefix(CharSequence
Returns the longest string
prefix such that
a.toString().startsWith(prefix) && b.toString().startsWith(prefix), taking care not to split surrogate pairs.
|
static String |
commonSuffix(CharSequence
Returns the longest string
suffix such that
a.toString().endsWith(suffix) && b.toString().endsWith(suffix), taking care not to split surrogate pairs.
|
static String |
emptyToNull(String
Returns the given string if it is nonempty;
null otherwise.
|
static boolean |
isNullOrEmpty(String
Returns
true if the given string is null or is the empty string.
|
static String |
nullToEmpty(String
Returns the given string if it is non-null; the empty string otherwise.
|
static String |
padEnd(String
Returns a string, of length at least
minLength, consisting of
string appended with as many copies of
padChar as are necessary to reach that length.
|
static String |
padStart(String
Returns a string, of length at least
minLength, consisting of
string prepended with as many copies of
padChar as are necessary to reach that length.
|
static String |
repeat(String
Returns a string consisting of a specific number of concatenated copies of an input string.
|
public static StringnullToEmpty(String string)
string - the string to test and possibly return
string itself if it is non-null;
"" if it is null
public static StringemptyToNull(String string)
null otherwise.
string - the string to test and possibly return
string itself if it is nonempty;
null if it is empty or null
public static boolean isNullOrEmpty(Stringstring)
true if the given string is null or is the empty string.
Consider normalizing your string references with nullToEmpty(java.lang.String). If you do, you can use String instead of this method, and you won't need special null-safe forms of methods like String either. Or, if you'd like to normalize "in the other direction," converting empty strings to null, you can use emptyToNull(java.lang.String).
string - a string reference to check
true if the string is null or is the empty string
public static StringpadStart(String string, int minLength, char padChar)
minLength, consisting of
string prepended with as many copies of
padChar as are necessary to reach that length. For example,
padStart("7", 3, '0') returns "007" padStart("2010", 3, '0') returns "2010" See Formatter for a richer set of formatting capabilities.
string - the string which should appear at the end of the result
minLength - the minimum length the resulting string must have. Can be zero or negative, in which case the input string is always returned.
padChar - the character to insert at the beginning of the result until the minimum length is reached
public static StringpadEnd(String string, int minLength, char padChar)
minLength, consisting of
string appended with as many copies of
padChar as are necessary to reach that length. For example,
padEnd("4.", 5, '0') returns "4.000" padEnd("2010", 3, '!') returns "2010" See Formatter for a richer set of formatting capabilities.
string - the string which should appear at the beginning of the result
minLength - the minimum length the resulting string must have. Can be zero or negative, in which case the input string is always returned.
padChar - the character to append to the end of the result until the minimum length is reached
public static Stringrepeat(String string, int count)
repeat("hey", 3) returns the string
"heyheyhey".
string - any non-null string
count - the number of times to repeat it; a nonnegative integer
string repeated
count times (the empty string if
count is zero)
IllegalArgumentException - if
count is negative
public static StringcommonPrefix(CharSequence a, CharSequence b)
prefix such that
a.toString().startsWith(prefix) && b.toString().startsWith(prefix), taking care not to split surrogate pairs. If
a and
b have no common prefix, returns the empty string.
public static StringcommonSuffix(CharSequence a, CharSequence b)
suffix such that
a.toString().endsWith(suffix) && b.toString().endsWith(suffix), taking care not to split surrogate pairs. If
a and
b have no common suffix, returns the empty string.