Class Strings
- java.lang.Object
-
- com.google.common.base.Strings
-
@GwtCompatible public final class Strings extends java.lang.Object
Static utility methods pertaining toStringorCharSequenceinstances.- Since:
- 3.0
- Author:
- Kevin Bourrillion
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringcommonPrefix(java.lang.CharSequence a, java.lang.CharSequence b)Returns the longest stringprefixsuch thata.toString().startsWith(prefix) && b.toString().startsWith(prefix), taking care not to split surrogate pairs.static java.lang.StringcommonSuffix(java.lang.CharSequence a, java.lang.CharSequence b)Returns the longest stringsuffixsuch thata.toString().endsWith(suffix) && b.toString().endsWith(suffix), taking care not to split surrogate pairs.static java.lang.StringemptyToNull(java.lang.String string)Returns the given string if it is nonempty;nullotherwise.static booleanisNullOrEmpty(java.lang.String string)Returnstrueif the given string is null or is the empty string.static java.lang.StringlenientFormat(java.lang.String template, @Nullable java.lang.Object... args)Returns the giventemplatestring with each occurrence of"%s"replaced with the corresponding argument value fromargs; or, if the placeholder and argument counts do not match, returns a best-effort form of that string.static java.lang.StringnullToEmpty(java.lang.String string)Returns the given string if it is non-null; the empty string otherwise.static java.lang.StringpadEnd(java.lang.String string, int minLength, char padChar)Returns a string, of length at leastminLength, consisting ofstringappended with as many copies ofpadCharas are necessary to reach that length.static java.lang.StringpadStart(java.lang.String string, int minLength, char padChar)Returns a string, of length at leastminLength, consisting ofstringprepended with as many copies ofpadCharas are necessary to reach that length.static java.lang.Stringrepeat(java.lang.String string, int count)Returns a string consisting of a specific number of concatenated copies of an input string.
-
-
-
Method Detail
-
nullToEmpty
public static java.lang.String nullToEmpty(@CheckForNull java.lang.String string)
Returns the given string if it is non-null; the empty string otherwise.- Parameters:
string- the string to test and possibly return- Returns:
stringitself if it is non-null;""if it is null
-
emptyToNull
@CheckForNull public static java.lang.String emptyToNull(@CheckForNull java.lang.String string)
Returns the given string if it is nonempty;nullotherwise.- Parameters:
string- the string to test and possibly return- Returns:
stringitself if it is nonempty;nullif it is empty or null
-
isNullOrEmpty
public static boolean isNullOrEmpty(@CheckForNull java.lang.String string)
Returnstrueif 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 useString.isEmpty()instead of this method, and you won't need special null-safe forms of methods likeString.toUpperCase(java.util.Locale)either. Or, if you'd like to normalize "in the other direction," converting empty strings tonull, you can useemptyToNull(java.lang.String).- Parameters:
string- a string reference to check- Returns:
trueif the string is null or is the empty string
-
padStart
public static java.lang.String padStart(java.lang.String string, int minLength, char padChar)
Returns a string, of length at leastminLength, consisting ofstringprepended with as many copies ofpadCharas are necessary to reach that length. For example,padStart("7", 3, '0')returns"007"padStart("2010", 3, '0')returns"2010"
See
Formatterfor a richer set of formatting capabilities.- Parameters:
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 returned.padChar- the character to insert at the beginning of the result until the minimum length is reached- Returns:
- the padded string
-
padEnd
public static java.lang.String padEnd(java.lang.String string, int minLength, char padChar)
Returns a string, of length at leastminLength, consisting ofstringappended with as many copies ofpadCharas are necessary to reach that length. For example,padEnd("4.", 5, '0')returns"4.000"padEnd("2010", 3, '!')returns"2010"
See
Formatterfor a richer set of formatting capabilities.- Parameters:
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 returned.padChar- the character to append to the end of the result until the minimum length is reached- Returns:
- the padded string
-
repeat
@InlineMe(replacement="string.repeat(count)") public static java.lang.String repeat(java.lang.String string, int count)
Returns a string consisting of a specific number of concatenated copies of an input string. For example,repeat("hey", 3)returns the string"heyheyhey".Java 11+ users: use
string.repeat(count)instead.- Parameters:
string- any non-null stringcount- the number of times to repeat it; a nonnegative integer- Returns:
- a string containing
stringrepeatedcounttimes (the empty string ifcountis zero) - Throws:
java.lang.IllegalArgumentException- ifcountis negative
-
commonPrefix
public static java.lang.String commonPrefix(java.lang.CharSequence a, java.lang.CharSequence b)
Returns the longest stringprefixsuch thata.toString().startsWith(prefix) && b.toString().startsWith(prefix), taking care not to split surrogate pairs. Ifaandbhave no common prefix, returns the empty string.- Since:
- 11.0
-
commonSuffix
public static java.lang.String commonSuffix(java.lang.CharSequence a, java.lang.CharSequence b)
Returns the longest stringsuffixsuch thata.toString().endsWith(suffix) && b.toString().endsWith(suffix), taking care not to split surrogate pairs. Ifaandbhave no common suffix, returns the empty string.- Since:
- 11.0
-
lenientFormat
public static java.lang.String lenientFormat(@CheckForNull java.lang.String template, @CheckForNull @Nullable java.lang.Object... args)
Returns the giventemplatestring with each occurrence of"%s"replaced with the corresponding argument value fromargs; or, if the placeholder and argument counts do not match, returns a best-effort form of that string. Will not throw an exception under normal conditions.Note: For most string-formatting needs, use
String.format,PrintWriter.format, and related methods. These support the full range of format specifiers, and alert you to usage errors by throwingIllegalFormatException.In certain cases, such as outputting debugging information or constructing a message to be used for another unchecked exception, an exception during string formatting would serve little purpose except to supplant the real information you were trying to provide. These are the cases this method is made for; it instead generates a best-effort string with all supplied argument values present. This method is also useful in environments such as GWT where
String.formatis not available. As an example, method implementations of thePreconditionsclass use this formatter, for both of the reasons just discussed.Warning: Only the exact two-character placeholder sequence
"%s"is recognized.- Parameters:
template- a string containing zero or more"%s"placeholder sequences.nullis treated as the four-character string"null".args- the arguments to be substituted into the message template. The first argument specified is substituted for the first occurrence of"%s"in the template, and so forth. Anullargument is converted to the four-character string"null"; non-null values are converted to strings usingObject.toString().- Since:
- 25.1
-
-