public final class Strings
extends java.lang.Object
String or CharSequence instances.| Modifier and Type | Method and Description |
|---|---|
static java.lang.String |
commonPrefix(java.lang.CharSequence a,
java.lang.CharSequence b) |
static java.lang.String |
commonSuffix(java.lang.CharSequence a,
java.lang.CharSequence b) |
static java.lang.String |
emptyToNull(java.lang.String string)
Returns the given string if it is nonempty;
null otherwise |
static java.lang.String |
format(java.lang.String template,
java.lang.Object... args)
Returns a formatted string using the specified template string and arguments.
|
static boolean |
isNullOrEmpty(java.lang.String string)
Returns
true if the given string is null or is the empty string. |
static java.lang.String |
nullToEmpty(java.lang.String string)
Returns the given string if it is non-null; the empty string otherwise
|
static java.lang.String |
padEnd(java.lang.String string,
int minLength,
char padChar)
Constructs 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 java.lang.String |
padStart(java.lang.String string,
int minLength,
char padChar)
Constructs 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 java.lang.String |
repeat(java.lang.String string,
int count)
Constructs a string consisting of a specific number of concatenated copies of an input string.
|
public static java.lang.String nullToEmpty(@Nullable
java.lang.String string)
string - The string to test and possibly returnstring itself if it is non-null; "" if it is null@Nullable
public static java.lang.String emptyToNull(@Nullable
java.lang.String string)
null otherwisestring - The string to test and possibly returnstring itself if it is nonempty; null if it is empty or nullpublic static boolean isNullOrEmpty(@Nullable
java.lang.String string)
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.isEmpty() instead of this method, and you won't need special null-safe forms of methods like
String.toUpperCase(java.util.Locale) 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 checktrue if the string is null or the empty string, false otherwisepublic static java.lang.String padStart(java.lang.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 resultminLength - The minimum length the resulting string must have. Can be zero or negative, in which case the input
string is always returnedpadChar - The character to insert at the beginning of the result until the minimum length is reachedminLength characters which ends with stringpublic static java.lang.String padEnd(java.lang.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 resultminLength - The minimum length the resulting string must have. Can be zero or negative, in which case the input
string is always returnedpadChar - The character to append to the end of the result until the minimum length is reachedminLength characters which starts with stringpublic static java.lang.String repeat(java.lang.String string,
int count)
repeat("hey", 3) returns the string "heyheyhey".string - A non-null string to repeatcount - The number of times to repeat the provided string; Must be nonnegativestring repeated count times, or the empty string if count is
zerojava.lang.IllegalArgumentException - If count is negativepublic static java.lang.String commonPrefix(java.lang.CharSequence a,
java.lang.CharSequence b)
a - One of two sequences to find the longest common prefix ofb - One of two sequences to find the longest common prefix ofprefix such that a.toString().startsWith(prefix) and
b.toString().startsWith(prefix), without splitting surrogate pairs. If a and b
have no common prefix, returns the empty stringpublic static java.lang.String commonSuffix(java.lang.CharSequence a,
java.lang.CharSequence b)
a - One of two sequences to find the longest common suffix ofb - One of two sequences to find the longest common suffix ofsuffix such that a.toString().endsWith(suffix) and
b.toString().endsWith(suffix), without splitting surrogate pairs. If a and b have
no common suffix, returns empty stringpublic static java.lang.String format(@Nullable
java.lang.String template,
@Nullable
java.lang.Object... args)
%s placeholder
substitution is supported
Provided in addition to the standard Java String.format(String, Object[]) for performance reasons - this
implementation is intended for more performance-sensitive applications
It is highly recommended to use this functionality where execution is deferred, such as operations which accept a
Supplier
template - A template for a formatted message. The message is formed by replacing each %s placeholder in
the template with an argument. These are matched by position - the first %s gets
args[0], etc. Unmatched arguments will be appended to the formatted message in square braces.
Unmatched placeholders will be left as-is.args - The arguments to be substituted into the message template. Arguments are converted to strings using
String.valueOf(Object).