Package com.google.common.base
Class Strings
- java.lang.Object
-
- com.google.common.base.Strings
-
@GwtCompatible public final class Strings extends java.lang.Object
Static utility methods pertaining toString
orCharSequence
instances.- Since:
- 3.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
commonPrefix(java.lang.CharSequence a, java.lang.CharSequence b)
Returns the longest stringprefix
such thata.toString().startsWith(prefix) && b.toString().startsWith(prefix)
, taking care not to split surrogate pairs.static java.lang.String
commonSuffix(java.lang.CharSequence a, java.lang.CharSequence b)
Returns the longest stringsuffix
such thata.toString().endsWith(suffix) && b.toString().endsWith(suffix)
, taking care not to split surrogate pairs.static java.lang.String
emptyToNull(java.lang.String string)
Returns the given string if it is nonempty;null
otherwise.static boolean
isNullOrEmpty(java.lang.String string)
Returnstrue
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)
Returns a string, of length at leastminLength
, consisting ofstring
appended with as many copies ofpadChar
as are necessary to reach that length.static java.lang.String
padStart(java.lang.String string, int minLength, char padChar)
Returns a string, of length at leastminLength
, consisting ofstring
prepended with as many copies ofpadChar
as are necessary to reach that length.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.
-
-
-
Method Detail
-
nullToEmpty
public static java.lang.String nullToEmpty(@Nullable 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:
string
itself if it is non-null;""
if it is null
-
emptyToNull
@Nullable public static java.lang.String emptyToNull(@Nullable java.lang.String string)
Returns the given string if it is nonempty;null
otherwise.- Parameters:
string
- the string to test and possibly return- Returns:
string
itself if it is nonempty;null
if it is empty or null
-
isNullOrEmpty
public static boolean isNullOrEmpty(@Nullable java.lang.String string)
Returnstrue
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 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:
true
if 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 ofstring
prepended with as many copies ofpadChar
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.- 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 ofstring
appended with as many copies ofpadChar
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.- 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
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"
.- Parameters:
string
- any non-null stringcount
- the number of times to repeat it; a nonnegative integer- Returns:
- a string containing
string
repeatedcount
times (the empty string ifcount
is zero) - Throws:
java.lang.IllegalArgumentException
- ifcount
is negative
-
commonPrefix
public static java.lang.String commonPrefix(java.lang.CharSequence a, java.lang.CharSequence b)
Returns the longest stringprefix
such thata.toString().startsWith(prefix) && b.toString().startsWith(prefix)
, taking care not to split surrogate pairs. Ifa
andb
have 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 stringsuffix
such thata.toString().endsWith(suffix) && b.toString().endsWith(suffix)
, taking care not to split surrogate pairs. Ifa
andb
have no common suffix, returns the empty string.- Since:
- 11.0
-
-