Top Description Constructors Methods
com.google.common.base

public final Class Strings

extends Object
Class Inheritance
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, java.util.logging.Level.WARNING

Static utility methods pertaining to String or CharSequence instances.
Author
Kevin Bourrillion
Since
3.0

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static String
commonPrefix(CharSequence a, CharSequence b)

Returns the longest string prefix such that a.toString().startsWith(prefix) && b.toString().startsWith(prefix), taking care not to split surrogate pairs.

public static String
commonSuffix(CharSequence a, CharSequence b)

Returns the longest string suffix such that a.toString().endsWith(suffix) && b.toString().endsWith(suffix), taking care not to split surrogate pairs.

public static String

Returns:

string itself if it is nonempty; null if it is empty or null
emptyToNull
(String
the string to test and possibly return
string
)

Returns the given string if it is nonempty; null otherwise.

public static boolean

Returns:

true if the string is null or is the empty string
isNullOrEmpty
(String
a string reference to check
string
)

Returns true if the given string is null or is the empty string.

public static String
lenientFormat(String
a string containing zero or more "%s" placeholder sequences. null is treated as the four-character string "null".
template
,
@Nullable Object...
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. A null argument is converted to the four-character string "null"; non-null values are converted to strings using Object#toString().
args
)

Returns the given template string with each occurrence of "%s" replaced with the corresponding argument value from args; or, if the placeholder and argument counts do not match, returns a best-effort form of that string.

private static String
public static String

Returns:

string itself if it is non-null; "" if it is null
nullToEmpty
(String
the string to test and possibly return
string
)

Returns the given string if it is non-null; the empty string otherwise.

public static String

Returns:

the padded string
padEnd
(String
the string which should appear at the beginning of the result
string
,
int
the minimum length the resulting string must have. Can be zero or negative, in which case the input string is always returned.
minLength
,
char
the character to append to the end of the result until the minimum length is reached
padChar
)

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.

public static String

Returns:

the padded string
padStart
(String
the string which should appear at the end of the result
string
,
int
the minimum length the resulting string must have. Can be zero or negative, in which case the input string is always returned.
minLength
,
char
the character to insert at the beginning of the result until the minimum length is reached
padChar
)

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.

public static String

Returns:

a string containing string repeated count times (the empty string if count is zero)
repeat
(String
any non-null string
string
,
int
the number of times to repeat it; a nonnegative integer
count
)

Returns a string consisting of a specific number of concatenated copies of an input string.

pack-priv static boolean
validSurrogatePairAt(CharSequence string, int index)

True when a valid surrogate pair starts at the given index in the given string.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Stringsback to summary
private Strings()

Method Detail

commonPrefixback to summary
public static String commonPrefix(CharSequence a, CharSequence b)

Returns the longest string 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.

Since
11.0
commonSuffixback to summary
public static String commonSuffix(CharSequence a, CharSequence b)

Returns the longest string 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.

Since
11.0
emptyToNullback to summary
public static String emptyToNull(String string)

Returns the given string if it is nonempty; null otherwise.

Parameters
string:String

the string to test and possibly return

Returns:String

string itself if it is nonempty; null if it is empty or null

Annotations
@CheckForNull
isNullOrEmptyback to summary
public static boolean isNullOrEmpty(String string)

Returns true if the given string is null or is the empty string.

Consider normalizing your string references with nullToEmpty. 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 either. Or, if you'd like to normalize "in the other direction," converting empty strings to null, you can use emptyToNull.

Parameters
string:String

a string reference to check

Returns:boolean

true if the string is null or is the empty string

lenientFormatback to summary
public static String lenientFormat(String template, @Nullable Object... args)

Returns the given template string with each occurrence of "%s" replaced with the corresponding argument value from args; 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 throwing java.util.IllegalFormatException.

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.format is not available. As an example, method implementations of the Preconditions class use this formatter, for both of the reasons just discussed.

Warning

Only the exact two-character placeholder sequence "%s" is recognized.

Parameters
template:String

a string containing zero or more "%s" placeholder sequences. null is treated as the four-character string "null".

args:@Nullable Object[]

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. A null argument is converted to the four-character string "null"; non-null values are converted to strings using Object#toString().

Since
25.1
lenientToStringback to summary
private static String lenientToString(Object o)
nullToEmptyback to summary
public static String nullToEmpty(String string)

Returns the given string if it is non-null; the empty string otherwise.

Parameters
string:String

the string to test and possibly return

Returns:String

string itself if it is non-null; "" if it is null

padEndback to summary
public static String padEnd(String string, int minLength, char padChar)

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. For example,

  • padEnd("4.", 5, '0') returns "4.000"
  • padEnd("2010", 3, '!') returns "2010"

See java.util.Formatter for a richer set of formatting capabilities.

Parameters
string:String

the string which should appear at the beginning of the result

minLength:int

the minimum length the resulting string must have. Can be zero or negative, in which case the input string is always returned.

padChar:char

the character to append to the end of the result until the minimum length is reached

Returns:String

the padded string

padStartback to summary
public static String padStart(String string, int minLength, char padChar)

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. For example,

  • padStart("7", 3, '0') returns "007"
  • padStart("2010", 3, '0') returns "2010"

See java.util.Formatter for a richer set of formatting capabilities.

Parameters
string:String

the string which should appear at the end of the result

minLength:int

the minimum length the resulting string must have. Can be zero or negative, in which case the input string is always returned.

padChar:char

the character to insert at the beginning of the result until the minimum length is reached

Returns:String

the padded string

repeatback to summary
public static String repeat(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:String

any non-null string

count:int

the number of times to repeat it; a nonnegative integer

Returns:String

a string containing string repeated count times (the empty string if count is zero)

Annotations
@InlineMe
replacement:string.repeat(count)
@InlineMeValidationDisabled:Java 11+ API only
Exceptions
IllegalArgumentException:
if count is negative
validSurrogatePairAtback to summary
pack-priv static boolean validSurrogatePairAt(CharSequence string, int index)

True when a valid surrogate pair starts at the given index in the given string. Out-of-range indexes return false.

Annotations
@VisibleForTesting