com.google.common.base
Class Strings

java.lang.Object
  extended by com.google.common.base.Strings

@GwtCompatible
public final class Strings
extends Object

Static utility methods pertaining to String or CharSequence instances.

Since:
3.0
Author:
Kevin Bourrillion

Method Summary
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.
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.
static String emptyToNull(String string)
          Returns the given string if it is nonempty; null otherwise.
static boolean isNullOrEmpty(String string)
          Returns true if the given string is null or is the empty string.
static String nullToEmpty(String string)
          Returns the given string if it is non-null; the empty string otherwise.
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.
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.
static String repeat(String string, int count)
          Returns a string consisting of a specific number of concatenated copies of an input string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

nullToEmpty

public static String nullToEmpty(@Nullable
                                 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 String emptyToNull(@Nullable
                                          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
                                    String string)
Returns 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).

Parameters:
string - a string reference to check
Returns:
true if the string is null or is the empty string

padStart

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,

See Formatter for a richer set of formatting capabilities.

Parameters:
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
Returns:
the padded string

padEnd

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,

See Formatter for a richer set of formatting capabilities.

Parameters:
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
Returns:
the padded string

repeat

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".

Parameters:
string - any non-null string
count - the number of times to repeat it; a nonnegative integer
Returns:
a string containing string repeated count times (the empty string if count is zero)
Throws:
IllegalArgumentException - if count is negative

commonPrefix

@Beta
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

commonSuffix

@Beta
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


Copyright © 2010-2012. All Rights Reserved.