Class KiwiStrings
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic StringblankToNull(String sequence) Returns a null if the input string is all whitespace characters or null.static StringAlias forformat(String, Object...).static StringSubstitutes each%sor{}intemplatewith an argument.static StringformatGuavaStyle(String template, Object... args) Same asformat(String, Object...)assuming Guava-style placeholders.static StringformatSlf4jJStyle(String template, Object... args) Same asformat(String, Object...)assuming SLF4J-style placeholders.nullSafeSplitOnCommas(@Nullable CharSequence sequence) Convenience method that splits the given comma-delimitedCharSequence, omitting any empty strings and trimming leading and trailing whitespace.nullSafeSplitToList(@Nullable CharSequence sequence) Splits the givenCharSequence, using aSPACEas the separator character, omitting any empty strings and trimming leading and trailing whitespace.nullSafeSplitToList(@Nullable CharSequence sequence, char separator) Splits the givenCharSequence, using the specified separator character, omitting any empty strings and trimming leading and trailing whitespace.nullSafeSplitToList(@Nullable CharSequence sequence, char separator, int maxGroups) Splits the givenCharSequence, using the specified separator character, into the maximum number of groups specified omitting any empty strings and trimming leading and trailing whitespace.nullSafeSplitToList(@Nullable CharSequence sequence, String separator) Splits the givenCharSequence, using the specified separator string, omitting any empty strings and trimming leading and trailing whitespace.nullSafeSplitToList(@Nullable CharSequence sequence, String separator, int maxGroups) Splits the givenCharSequence, using the specified separator string, into the maximum number of groups specified omitting any empty strings and trimming leading and trailing whitespace.nullSafeSplitWithTrimAndOmitEmpty(@Nullable CharSequence sequence) Splits the givenCharSequence, using aSPACEas the separator character, omitting any empty strings and trimming leading and trailing whitespace.nullSafeSplitWithTrimAndOmitEmpty(@Nullable CharSequence sequence, char separator) Splits the givenCharSequence, using the specified separator character, omitting any empty strings and trimming leading and trailing whitespace.nullSafeSplitWithTrimAndOmitEmpty(@Nullable CharSequence sequence, String separator) Splits the givenCharSequence, using the specified separator string, omitting any empty strings and trimming leading and trailing whitespace.splitOnCommas(CharSequence sequence) Convenience method that splits the given comma-delimitedCharSequence, omitting any empty strings and trimming leading and trailing whitespace.splitToList(CharSequence sequence) Splits the givenCharSequence, using aSPACEas the separator character, omitting any empty strings and trimming leading and trailing whitespace.splitToList(CharSequence sequence, char separator) Splits the givenCharSequence, using the specified separator character, omitting any empty strings and trimming leading and trailing whitespace.splitToList(CharSequence sequence, char separator, int maxGroups) Splits the givenCharSequence, using the specified separator character, into the maximum number of groups specified omitting any empty strings and trimming leading and trailing whitespace.splitToList(CharSequence sequence, String separator) Splits the givenCharSequence, using the specified separator string, omitting any empty strings and trimming leading and trailing whitespace.splitToList(CharSequence sequence, String separator, int maxGroups) Splits the givenCharSequence, using the specified separator string, into the maximum number of groups specified omitting any empty strings and trimming leading and trailing whitespace.splitWithTrimAndOmitEmpty(CharSequence sequence) Splits the givenCharSequence, using aSPACEas the separator character, omitting any empty strings and trimming leading and trailing whitespace.splitWithTrimAndOmitEmpty(CharSequence sequence, char separator) Splits the givenCharSequence, using the specified separator character, omitting any empty strings and trimming leading and trailing whitespace.splitWithTrimAndOmitEmpty(CharSequence sequence, String separator) Splits the givenCharSequence, using the specified separator string, omitting any empty strings and trimming leading and trailing whitespace.
-
Field Details
-
SPACE
public static final char SPACEA space character.- See Also:
-
TAB
public static final char TABA tab character.- See Also:
-
COMMA
public static final char COMMAA comma character.- See Also:
-
NEWLINE
public static final char NEWLINEA newline character.- See Also:
-
-
Method Details
-
splitWithTrimAndOmitEmpty
Splits the givenCharSequence, using aSPACEas the separator character, omitting any empty strings and trimming leading and trailing whitespace.- Parameters:
sequence- the character sequence to be split- Returns:
- an Iterable over the split strings
- See Also:
-
nullSafeSplitWithTrimAndOmitEmpty
Splits the givenCharSequence, using aSPACEas the separator character, omitting any empty strings and trimming leading and trailing whitespace.- Parameters:
sequence- the character sequence to be split, may be null- Returns:
- an Iterable over the split strings, or an empty Iterable if
sequenceis blank - See Also:
-
splitWithTrimAndOmitEmpty
Splits the givenCharSequence, using the specified separator character, omitting any empty strings and trimming leading and trailing whitespace.- Parameters:
sequence- the character sequence to be splitseparator- the separator character to use- Returns:
- an Iterable over the split strings
-
nullSafeSplitWithTrimAndOmitEmpty
public static Iterable<String> nullSafeSplitWithTrimAndOmitEmpty(@Nullable CharSequence sequence, char separator) Splits the givenCharSequence, using the specified separator character, omitting any empty strings and trimming leading and trailing whitespace.- Parameters:
sequence- the character sequence to be split, may be nullseparator- the separator character to use- Returns:
- an Iterable over the split strings, or an empty Iterable if
sequenceis blank
-
splitWithTrimAndOmitEmpty
Splits the givenCharSequence, using the specified separator string, omitting any empty strings and trimming leading and trailing whitespace.- Parameters:
sequence- the character sequence to be splitseparator- the separator to use, e.g.", "- Returns:
- an Iterable over the split strings
-
nullSafeSplitWithTrimAndOmitEmpty
public static Iterable<String> nullSafeSplitWithTrimAndOmitEmpty(@Nullable CharSequence sequence, String separator) Splits the givenCharSequence, using the specified separator string, omitting any empty strings and trimming leading and trailing whitespace.- Parameters:
sequence- the character sequence to be split, may be nullseparator- the separator to use, e.g.", "- Returns:
- an Iterable over the split strings, or an empty Iterable if
sequenceis blank
-
splitToList
Splits the givenCharSequence, using aSPACEas the separator character, omitting any empty strings and trimming leading and trailing whitespace. Returns an immutable list.- Parameters:
sequence- the character sequence to be split- Returns:
- an immutable list containing the split strings
- See Also:
-
nullSafeSplitToList
Splits the givenCharSequence, using aSPACEas the separator character, omitting any empty strings and trimming leading and trailing whitespace. Returns an immutable list.- Parameters:
sequence- the character sequence to be split, may be null- Returns:
- an immutable list containing the split strings, or an empty list if
sequenceis blank - See Also:
-
splitToList
Splits the givenCharSequence, using the specified separator character, omitting any empty strings and trimming leading and trailing whitespace. Returns an immutable list.- Parameters:
sequence- the character sequence to be splitseparator- the separator character to use- Returns:
- an immutable list containing the split strings
- See Also:
-
nullSafeSplitToList
Splits the givenCharSequence, using the specified separator character, omitting any empty strings and trimming leading and trailing whitespace. Returns an immutable list.- Parameters:
sequence- the character sequence to be split, may be nullseparator- the separator character to use- Returns:
- an immutable list containing the split strings, or an empty list if
sequenceis blank - See Also:
-
splitToList
Splits the givenCharSequence, using the specified separator character, into the maximum number of groups specified omitting any empty strings and trimming leading and trailing whitespace. Returns an immutable list.- Parameters:
sequence- the character sequence to be splitseparator- the separator character to usemaxGroups- the maximum number of groups to separate into- Returns:
- an immutable list containing the split strings
-
nullSafeSplitToList
public static List<String> nullSafeSplitToList(@Nullable CharSequence sequence, char separator, int maxGroups) Splits the givenCharSequence, using the specified separator character, into the maximum number of groups specified omitting any empty strings and trimming leading and trailing whitespace. Returns an immutable list.- Parameters:
sequence- the character sequence to be split, may be nullseparator- the separator character to usemaxGroups- the maximum number of groups to separate into- Returns:
- an immutable list containing the split strings, or an empty list if
sequenceis blank
-
splitToList
Splits the givenCharSequence, using the specified separator string, omitting any empty strings and trimming leading and trailing whitespace. Returns an immutable list.- Parameters:
sequence- the character sequence to be splitseparator- the separator string to use- Returns:
- an immutable list containing the split strings
-
nullSafeSplitToList
Splits the givenCharSequence, using the specified separator string, omitting any empty strings and trimming leading and trailing whitespace. Returns an immutable list.- Parameters:
sequence- the character sequence to be split, may be nullseparator- the separator string to use- Returns:
- an immutable list containing the split strings, or an empty list if
sequenceis blank
-
splitToList
Splits the givenCharSequence, using the specified separator string, into the maximum number of groups specified omitting any empty strings and trimming leading and trailing whitespace. Returns an immutable list.- Parameters:
sequence- the character sequence to be splitseparator- the separator string to usemaxGroups- the maximum number of groups to separate into- Returns:
- an immutable list containing the split strings
-
nullSafeSplitToList
public static List<String> nullSafeSplitToList(@Nullable CharSequence sequence, String separator, int maxGroups) Splits the givenCharSequence, using the specified separator string, into the maximum number of groups specified omitting any empty strings and trimming leading and trailing whitespace. Returns an immutable list.- Parameters:
sequence- the character sequence to be split, may be nullseparator- the separator string to usemaxGroups- the maximum number of groups to separate into- Returns:
- an immutable list containing the split strings, or an empty list if
sequenceis blank
-
splitOnCommas
Convenience method that splits the given comma-delimitedCharSequence, omitting any empty strings and trimming leading and trailing whitespace. Returns an immutable list.- Parameters:
sequence- the character sequence to be split- Returns:
- an immutable list containing the split strings
- See Also:
-
nullSafeSplitOnCommas
Convenience method that splits the given comma-delimitedCharSequence, omitting any empty strings and trimming leading and trailing whitespace. Returns an immutable list.- Parameters:
sequence- the character sequence to be split, may be null- Returns:
- an immutable list containing the split strings, or an empty list if
sequenceis blank - See Also:
-
blankToNull
Returns a null if the input string is all whitespace characters or null.- Parameters:
sequence- a possibly null, blank, or zero length String.- Returns:
- null if
sequenceis blank, otherwise returnsequence
-
format
Substitutes each%sor{}intemplatewith an argument. These are matched by position: the first%s(or{}) getsargs[0], etc. If there are more arguments than placeholders, the unmatched arguments will be appended to the end of the formatted message in square braces.This method currently accepts either
%sor{}but not both at the same time. It won't work if you mix and match them as that is confusing anyway. What will happen is that only the%splaceholders will be resolved, and the{}will appear as a literal{}and the resulting message will thus be very difficult to understand, as there will be more arguments than%splaceholders.Generally you should pick one style and be consistent throughout your entire application. Since originally this method only supported the Guava
%s, this support was retained for obvious backward-compatibility reasons, and the SLF4J{}style as added because we kept coming across instances where people are used to SLF4J replacement parameter style and used that, thus making the message not interpolate correctly (though thanks to Guava's implementation, all the parameter values are still displayed after the message as extra parameters).This method was originally copied directly from Guava 18.0's
com.google.common.base.Preconditions#format(String, Object...)because it was not public in Guava, and it is useful and provides better performance than using theString.format(java.util.Locale, String, Object...)method. A slight modification we made is to not re-assign thetemplateargument. Guava 25.1 moved this very useful functionality into the GuavaStringsclass as {Strings.lenientFormat(String, Object...)}. However, it only accepts%sas the replacement placeholder. For performance comparisons of the JDKString.format(String, Object...)method, see Performance: Java's String.format [duplicate] on Stack Overflow as well as Should I use Java's String.format() if performance is important?- Parameters:
template- a non-null string containing 0 or more%sor{}placeholders.args- the arguments to be substituted into the message template. Arguments are converted to strings usingString.valueOf(Object). Arguments can be null.- Returns:
- the formatted string after making replacements
-
f
Alias forformat(String, Object...).- Parameters:
template- a non-null string containing 0 or more%sor{}placeholders.args- the arguments to be substituted into the message template. Arguments are converted to strings usingString.valueOf(Object). Arguments can be null.- Returns:
- the formatted string after making replacements
-
formatGuavaStyle
Same asformat(String, Object...)assuming Guava-style placeholders.- Parameters:
template- a non-null string containing 0 or more%splaceholders.args- the arguments to be substituted into the message template. Arguments are converted to strings usingString.valueOf(Object). Arguments can be null.- Returns:
- the formatted string after making replacements
- See Also:
-
formatSlf4jJStyle
Same asformat(String, Object...)assuming SLF4J-style placeholders.- Parameters:
template- a non-null string containing 0 or more{}placeholders.args- the arguments to be substituted into the message template. Arguments are converted to strings usingString.valueOf(Object). Arguments can be null.- Returns:
- the formatted string after making replacements
- See Also:
-