public class Strings
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
EMPTY
The empty
String . |
static CharMatcher |
IS_JAVA_IDENTIFIER_PART
A
CharMatcher that matches valid Java identifier characters. |
static java.lang.String |
VALID_NON_ALPHANUM_FILE_CHARS
Valid non alphanumeric characters for filenames.
|
Constructor and Description |
---|
Strings() |
Modifier and Type | Method and Description |
---|---|
static void |
checkNonEmpty(java.lang.CharSequence s)
throws IllegalArgument if string not empty; cf.
|
static void |
checkNonEmpty(java.lang.CharSequence s,
java.lang.String message)
throws IllegalArgument if string not empty; cf.
|
static java.lang.String |
collapseWhitespace(java.lang.String x,
java.lang.String whitespaceReplacement)
replaces each sequence of whitespace in the first string with the replacement in the second string
|
static java.lang.String[] |
combineArrays(java.lang.String[]... arrays) |
static boolean |
containsAny(java.lang.CharSequence input,
java.lang.CharSequence... candidates) |
static boolean |
containsLiteral(java.lang.CharSequence input,
java.lang.CharSequence fragment) |
static boolean |
containsLiteralIgnoreCase(java.lang.CharSequence input,
java.lang.CharSequence fragment) |
static int |
countOccurrences(java.lang.String phrase,
char target) |
static java.lang.String |
emptyToNull(java.lang.String var) |
static FormattedString |
format(java.lang.String pattern,
java.lang.Object... args)
wraps a call to
String.format(String, Object...) in a toString, i.e. |
static char |
getDecimalSeparator(java.util.Locale locale) |
static char |
getDefaultDecimalSeparator() |
static java.lang.String |
getFirstLine(java.lang.String s) |
static java.lang.String |
getFirstWord(java.lang.String s)
returns the first word (whitespace delimited text), or null if there is none (input null or all whitespace)
|
static java.lang.String |
getFirstWordAfter(java.lang.String context,
java.lang.String phrase)
returns the first word after the given phrase, or null if no such phrase;
if the character immediately after the phrase is not whitespace, the non-whitespace
sequence starting with that character will be returned
|
static java.lang.String |
getFragmentBetween(java.lang.String input,
java.lang.String prefix,
java.lang.String suffix)
looks for first section of text in following the prefix and, if present, before the suffix;
null if the prefix is not present in the string, and everything after the prefix if suffix is not present in the string;
if either prefix or suffix is null, it is treated as the start/end of the string
|
static java.lang.String |
getLastWord(java.lang.String s)
returns the last word (whitespace delimited text), or null if there is none (input null or all whitespace)
|
static java.lang.String |
getRemainderOfLineAfter(java.lang.String context,
java.lang.String phrase)
searches in context for the given phrase, and returns the untrimmed remainder of the first line
on which the phrase is found
|
static int |
getWordCount(java.lang.String phrase,
boolean respectQuotes) |
static java.lang.String |
ies(int count)
returns "ies" if the argument is not 1, "y" otherwise; useful when constructing plurals
|
static java.lang.String |
ies(java.lang.Iterable<?> x)
as
ies(int) based on size of argument |
static java.lang.String |
ies(java.util.Iterator<?> x)
as
ies(int) based on size of argument |
static java.lang.String |
ies(java.util.Map<?,?> x)
as
ies(int) based on size of argument |
static boolean |
isBlank(java.lang.CharSequence s)
Checks if the given string is empty or only consists of whitespace.
|
static boolean |
isEmpty(java.lang.CharSequence s)
Checks if the given string is null or is an empty string.
|
static boolean |
isLowerCase(java.lang.String s) |
static boolean |
isMultiLine(java.lang.String s) |
static boolean |
isNonBlank(java.lang.CharSequence s)
The inverse of
isBlank(CharSequence) . |
static boolean |
isNonEmpty(java.lang.CharSequence s)
The inverse of
isEmpty(CharSequence) . |
static java.lang.String |
join(java.lang.Iterable<? extends java.lang.Object> list,
java.lang.String separator)
convenience for
Joiner |
static java.lang.String |
join(java.lang.Object[] list,
java.lang.String separator)
convenience for
Joiner |
static <any> |
lengthComparator()
returns comparator which compares based on length, with shorter ones first (and null before that);
in event of a tie, it uses the toString order
|
static java.lang.String |
lines(java.lang.Iterable<java.lang.String> lines)
convenience for joining lines together
|
static java.lang.String |
lines(java.lang.String... lines)
convenience for joining lines together
|
static java.lang.String[] |
makeArray(java.lang.String prefix,
int count)
returns e.g.
|
static java.lang.String |
makeISOSizeString(long sizeInBytes)
Returns a size string using ISO suffixes from
ByteSizeStrings.iso() , e.g. |
static java.lang.String |
makeJavaSizeString(long sizeInBytes)
Returns a size string using Java suffixes from
ByteSizeStrings.java() , e.g. |
static java.lang.String |
makePaddedString(java.lang.String base,
int len,
java.lang.String left_pad,
java.lang.String right_pad)
pads the string with "pad" at the left up to len; no padding if base longer than len
|
static java.lang.String |
makeRandomId(int l) |
static java.lang.String |
makeRealString(double x,
int maxlen,
int prec,
int leftPadLen)
creates a string from a real number, with specified accuracy (more iff it comes for free, ie integer-part);
switches to E notation if needed to fit within maxlen; can be padded left up too (not useful)
|
static java.lang.String |
makeRealString(double x,
int maxlen,
int prec,
int leftPadLen,
double skipDecimalThreshhold,
boolean useEForSmallNumbers)
creates a string from a real number, with specified accuracy (more iff it comes for free, ie integer-part);
switches to E notation if needed to fit within maxlen; can be padded left up too (not useful)
|
static java.lang.String |
makeRealStringNearZero(double x,
int maxlen,
int prec,
int leftPadLen)
creates a string from a real number, with specified accuracy (more iff it comes for free, ie integer-part);
switches to E notation if needed to fit within maxlen; can be padded left up too (not useful)
|
static java.lang.String |
makeRepeated(char c,
int length) |
static java.lang.String |
makeSizeString(long sizeInBytes)
Returns a size string using metric suffixes from
ByteSizeStrings.metric() , e.g. |
static java.lang.String |
makeTimeString(long utcMillis)
Deprecated.
use
Time#makeTimeStringRounded(long) |
static java.lang.String |
makeValidFilename(java.lang.String s)
Returns a valid filename based on the input.
|
static java.lang.String |
makeValidJavaName(java.lang.String s)
Returns a valid Java identifier name based on the input.
|
static java.lang.String |
makeValidUniqueJavaName(java.lang.String s)
Returns a unique valid java identifier name based on the input.
|
static java.lang.String |
makeZeroPaddedString(int i,
int len)
pads the string with 0's at the left up to len; no padding if i longer than len
|
static java.lang.String |
maxlen(java.lang.String s,
int maxlen)
returns up to maxlen characters from the start of s
|
static java.lang.String |
maxlenWithEllipsis(java.lang.String s,
int maxlen)
as {@link #maxlenWithEllipsis(String, int, String) with "..." as the ellipsis
|
static java.lang.String |
maxlenWithEllipsis(java.lang.String s,
int maxlen,
java.lang.String ellipsis)
as {@link #maxlenWithEllipsis(String, int) but replacing the last few chars with the given ellipsis
|
static <T extends java.lang.CharSequence> |
maybeNonBlank(T s) |
static java.util.List<java.lang.String> |
parseCsv(java.lang.String csv) |
static java.util.List<java.lang.String> |
parseCsv(java.lang.String csv,
java.lang.String separatorRegex)
Parses a string as comma separated values and returns a list of the entries with whitespace removed.
|
static java.lang.String |
removeAfter(java.lang.String string,
java.lang.String marker,
boolean includeMarker)
Removes everything after the marker, optionally also removing the marker.
|
static java.lang.String |
removeAllFromEnd(java.lang.String string,
java.lang.String... suffixes)
As removeFromEnd, but repeats until all such suffixes are gone
|
static java.lang.String |
removeAllFromStart(java.lang.String string,
java.lang.String... prefixes)
As
removeFromStart(String, String) , repeating until all such prefixes are gone. |
static java.lang.String |
removeBefore(java.lang.String string,
java.lang.String marker,
boolean includeMarker)
Removes everything before the marker, optionally also removing the marker.
|
static java.lang.String |
removeFromEnd(java.lang.String string,
java.lang.String suffix)
Removes suffix from the end of the string.
|
static java.lang.String |
removeFromStart(java.lang.String string,
java.lang.String prefix)
Removes prefix from the beginning of string.
|
static java.lang.String |
removeLines(java.lang.String multiline,
<any> patternToRemove) |
static java.lang.String |
repeat(java.lang.String base,
int count)
returns base repeated count times
|
static java.lang.String |
replaceAll(java.lang.String source,
java.util.Map replacements)
NON-REGEX - replaces all key->value entries from the replacement map in source (non-regex)
|
static java.lang.String |
replaceAll(java.lang.String source,
java.lang.String pattern,
java.lang.String replacement)
NON-REGEX replaceAll - see the better, explicitly named
replaceAllNonRegex(String, String, String) . |
static java.lang.String |
replaceAllNonRegex(java.lang.String source,
java.lang.String pattern,
java.lang.String replacement)
Replaces all instances in source, of the given pattern, with the given replacement
(not interpreting any arguments as regular expressions).
|
static java.lang.String |
replaceAllRegex(java.lang.String source,
java.lang.String pattern,
java.lang.String replacement)
REGEX replacement -- explicit method name for readability, doing same as
String.replaceAll(String, String) . |
static java.lang.String |
reverse(java.lang.String name) |
static java.lang.String |
s(int count)
returns "s" if the argument is not 1, empty string otherwise; useful when constructing plurals
|
static java.lang.String |
s(java.lang.Iterable<?> x)
as
s(int) based on size of argument |
static java.lang.String |
s(java.util.Iterator<?> x)
as
s(int) based on size of argument |
static java.lang.String |
s(java.util.Map<?,?> x)
as
s(int) based on size of argument |
static StringShortener |
shortener()
returns a configurable shortener
|
static java.lang.String |
toInitialCapOnly(java.lang.String value) |
static java.lang.String |
toLowerCase(java.lang.String value) |
static java.lang.String |
toString(java.lang.Object o)
returns toString of the object if it is not null, otherwise null
|
static java.util.List<java.lang.String> |
toStringList(java.util.List<?> list,
java.lang.String valueIfNull)
converts a list of any objects to a list of strings, using
Object.toString() ,
with the second argument used where an entry is null |
static java.util.List<java.lang.String> |
toStringList(java.lang.Object v)
Tries to convert v to a list of strings.
|
static java.util.Map<java.lang.String,java.lang.String> |
toStringMap(java.util.Map<?,?> map,
java.lang.String valueIfNull)
converts a map of any objects to a map of strings, using
Object.toString() ,
with the second argument used where a value (or key) is null |
static <any> |
toStringSupplier(java.lang.Object src) |
static java.lang.String |
toStringWithValueForNull(java.lang.Object o,
java.lang.String valueIfNull)
returns toString of the object if it is not null, otherwise the given value
|
static java.lang.String |
toUniqueString(java.lang.Object x,
int optionalMax)
Returns canonicalized string from the given object, made "unique" by:
putting sets into the toString order
appending a hash code if it's longer than the max (and the max is bigger than 0)
|
static java.lang.String |
trim(java.lang.String s) |
static void |
trimAll(java.lang.String[] s) |
static java.lang.String |
trimEnd(java.lang.String s) |
public static final java.lang.String EMPTY
String
.public static final java.lang.String VALID_NON_ALPHANUM_FILE_CHARS
public static final CharMatcher IS_JAVA_IDENTIFIER_PART
CharMatcher
that matches valid Java identifier characters.Character.isJavaIdentifierPart(char)
public static boolean isEmpty(java.lang.CharSequence s)
s
- the String to checkisNonEmpty(CharSequence)
,
isBlank(CharSequence)
,
isNonBlank(CharSequence)
public static boolean isBlank(java.lang.CharSequence s)
s
- the String to checkisEmpty(CharSequence)
,
isNonEmpty(CharSequence)
,
isNonBlank(CharSequence)
public static boolean isNonEmpty(java.lang.CharSequence s)
isEmpty(CharSequence)
.s
- the String to checkisEmpty(CharSequence)
,
isBlank(CharSequence)
,
isNonBlank(CharSequence)
public static boolean isNonBlank(java.lang.CharSequence s)
isBlank(CharSequence)
.s
- the String to checkisEmpty(CharSequence)
,
isNonEmpty(CharSequence)
,
isBlank(CharSequence)
public static <T extends java.lang.CharSequence> Maybe<T> maybeNonBlank(T s)
Maybe
object which is absent if the argument isBlank(CharSequence)
public static void checkNonEmpty(java.lang.CharSequence s)
public static void checkNonEmpty(java.lang.CharSequence s, java.lang.String message)
public static java.lang.String removeFromEnd(java.lang.String string, java.lang.String suffix)
public static java.lang.String removeAllFromEnd(java.lang.String string, java.lang.String... suffixes)
public static java.lang.String removeFromStart(java.lang.String string, java.lang.String prefix)
public static java.lang.String removeAllFromStart(java.lang.String string, java.lang.String... prefixes)
removeFromStart(String, String)
, repeating until all such prefixes are gone.public static java.lang.String removeAfter(java.lang.String string, java.lang.String marker, boolean includeMarker)
public static java.lang.String removeBefore(java.lang.String string, java.lang.String marker, boolean includeMarker)
public static java.lang.String join(java.lang.Iterable<? extends java.lang.Object> list, java.lang.String separator)
Joiner
public static java.lang.String join(java.lang.Object[] list, java.lang.String separator)
Joiner
public static java.lang.String lines(java.lang.String... lines)
public static java.lang.String lines(java.lang.Iterable<java.lang.String> lines)
public static java.lang.String removeLines(java.lang.String multiline, <any> patternToRemove)
public static java.lang.String replaceAll(java.lang.String source, java.util.Map replacements)
public static java.lang.String replaceAll(java.lang.String source, java.lang.String pattern, java.lang.String replacement)
replaceAllNonRegex(String, String, String)
.public static java.lang.String replaceAllNonRegex(java.lang.String source, java.lang.String pattern, java.lang.String replacement)
This is actually the same as the very ambiguous String.replace(CharSequence, CharSequence)
,
which does replace all, but not using regex like the similarly ambiguous String.replaceAll(String, String)
as.
Alternatively see replaceAllRegex(String, String, String)
.
public static java.lang.String replaceAllRegex(java.lang.String source, java.lang.String pattern, java.lang.String replacement)
String.replaceAll(String, String)
.public static java.lang.String makeValidFilename(java.lang.String s)
VALID_NON_ALPHANUM_FILE_CHARS
,
with any runs of invalid characters being replaced by _.java.lang.NullPointerException
- if the input string is null.java.lang.IllegalArgumentException
- if the input string is blank.public static java.lang.String makeValidJavaName(java.lang.String s)
The result is usually unique to s, though this isn't guaranteed, for example if
all characters are invalid. For a unique identifier use makeValidUniqueJavaName(String)
.
makeValidUniqueJavaName(String)
public static java.lang.String makeValidUniqueJavaName(java.lang.String s)
makeValidJavaName(String)
but with String.hashCode()
appended where necessary to guarantee uniqueness.makeValidJavaName(String)
public static java.lang.String makeRandomId(int l)
Identifiers#makeRandomId(int)}
public static java.lang.String makeZeroPaddedString(int i, int len)
public static java.lang.String makePaddedString(java.lang.String base, int len, java.lang.String left_pad, java.lang.String right_pad)
public static void trimAll(java.lang.String[] s)
public static java.lang.String makeRealString(double x, int maxlen, int prec, int leftPadLen)
x
- number to usemaxlen
- maximum length for the numeric string, if possible (-1 to suppress)prec
- number of digits accuracy desired (more kept for integers)leftPadLen
- will add spaces at left if necessary to make string this long (-1 to suppress) [probably not usef]public static java.lang.String makeRealString(double x, int maxlen, int prec, int leftPadLen, double skipDecimalThreshhold, boolean useEForSmallNumbers)
x
- number to usemaxlen
- maximum length for the numeric string, if possible (-1 to suppress)prec
- number of digits accuracy desired (more kept for integers)leftPadLen
- will add spaces at left if necessary to make string this long (-1 to suppress) [probably not usef]skipDecimalThreshhold
- if positive it will not add a decimal part if the fractional part is less than this threshhold
(but for a value 3.00001 it would show zeroes, e.g. with 3 precision and positive threshhold <= 0.00001 it would show 3.00);
if zero or negative then decimal digits are always shownuseEForSmallNumbers
- whether to use E notation for numbers near zero (e.g. 0.001)public static java.lang.String makeRealStringNearZero(double x, int maxlen, int prec, int leftPadLen)
x
- number to usemaxlen
- maximum length for the numeric string, if possible (-1 to suppress)prec
- number of digits accuracy desired (more kept for integers)leftPadLen
- will add spaces at left if necessary to make string this long (-1 to suppress) [probably not usef]public static java.lang.String getFirstWord(java.lang.String s)
public static java.lang.String getLastWord(java.lang.String s)
public static java.lang.String getFirstWordAfter(java.lang.String context, java.lang.String phrase)
public static java.lang.String getRemainderOfLineAfter(java.lang.String context, java.lang.String phrase)
@Deprecated public static java.lang.String makeTimeString(long utcMillis)
Time#makeTimeStringRounded(long)
public static java.lang.String[] makeArray(java.lang.String prefix, int count)
public static java.lang.String[] combineArrays(java.lang.String[]... arrays)
public static java.lang.String toInitialCapOnly(java.lang.String value)
public static java.lang.String reverse(java.lang.String name)
public static boolean isLowerCase(java.lang.String s)
public static java.lang.String makeRepeated(char c, int length)
public static java.lang.String trim(java.lang.String s)
public static java.lang.String trimEnd(java.lang.String s)
public static java.lang.String maxlen(java.lang.String s, int maxlen)
public static java.lang.String maxlenWithEllipsis(java.lang.String s, int maxlen)
public static java.lang.String maxlenWithEllipsis(java.lang.String s, int maxlen, java.lang.String ellipsis)
public static java.lang.String toString(java.lang.Object o)
public static java.lang.String toStringWithValueForNull(java.lang.Object o, java.lang.String valueIfNull)
public static boolean containsLiteralIgnoreCase(java.lang.CharSequence input, java.lang.CharSequence fragment)
public static boolean containsLiteral(java.lang.CharSequence input, java.lang.CharSequence fragment)
public static boolean containsAny(java.lang.CharSequence input, java.lang.CharSequence... candidates)
public static java.lang.String makeSizeString(long sizeInBytes)
ByteSizeStrings.metric()
, e.g. 23.5MBpublic static java.lang.String makeISOSizeString(long sizeInBytes)
ByteSizeStrings.iso()
, e.g. 23.5MiBpublic static java.lang.String makeJavaSizeString(long sizeInBytes)
ByteSizeStrings.java()
, e.g. 23mpublic static StringShortener shortener()
public static <any> toStringSupplier(java.lang.Object src)
public static FormattedString format(java.lang.String pattern, java.lang.Object... args)
String.format(String, Object...)
in a toString, i.e. using %s syntax,
useful for places where we want deferred evaluation
(e.g. as message to Preconditions
to skip concatenation when not needed)public static java.lang.String s(int count)
public static java.lang.String s(@Nullable java.util.Map<?,?> x)
s(int)
based on size of argumentpublic static java.lang.String s(java.lang.Iterable<?> x)
s(int)
based on size of argumentpublic static java.lang.String s(java.util.Iterator<?> x)
s(int)
based on size of argumentpublic static java.lang.String ies(int count)
public static java.lang.String ies(@Nullable java.util.Map<?,?> x)
ies(int)
based on size of argumentpublic static java.lang.String ies(java.lang.Iterable<?> x)
ies(int)
based on size of argumentpublic static java.lang.String ies(java.util.Iterator<?> x)
ies(int)
based on size of argumentpublic static java.util.Map<java.lang.String,java.lang.String> toStringMap(java.util.Map<?,?> map, java.lang.String valueIfNull)
Object.toString()
,
with the second argument used where a value (or key) is nullpublic static java.util.List<java.lang.String> toStringList(java.util.List<?> list, java.lang.String valueIfNull)
Object.toString()
,
with the second argument used where an entry is nullpublic static java.util.List<java.lang.String> toStringList(java.lang.Object v)
If v is null
then the method returns an empty immutable list.
If v is Iterable
or an Object[]
then toString is called on its elements.
If v is a String
then a singleton list containing v is returned.
Otherwise the method throws an IllegalArgumentException
.
public static java.lang.String repeat(java.lang.String base, int count)
public static <any> lengthComparator()
public static boolean isMultiLine(java.lang.String s)
public static java.lang.String getFirstLine(java.lang.String s)
public static java.lang.String getFragmentBetween(java.lang.String input, java.lang.String prefix, java.lang.String suffix)
public static int getWordCount(java.lang.String phrase, boolean respectQuotes)
public static char getDecimalSeparator(java.util.Locale locale)
public static char getDefaultDecimalSeparator()
public static java.lang.String collapseWhitespace(java.lang.String x, java.lang.String whitespaceReplacement)
public static java.lang.String toLowerCase(java.lang.String value)
public static java.lang.String emptyToNull(java.lang.String var)
public static java.lang.String toUniqueString(java.lang.Object x, int optionalMax)
public static java.util.List<java.lang.String> parseCsv(java.lang.String csv, java.lang.String separatorRegex)
csv
- The "comma" separated values.separatorRegex
- Regex of separatorRegex.public static java.util.List<java.lang.String> parseCsv(java.lang.String csv)
public static int countOccurrences(java.lang.String phrase, char target)