Class KiwiStrings
- java.lang.Object
-
- org.kiwiproject.base.KiwiStrings
-
public final class KiwiStrings extends Object
Utility methods relating to strings or similar.
-
-
Constructor Summary
Constructors Constructor Description KiwiStrings()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringblankToNull(String sequence)Returns a null if the input string is all whitespace characters or null.static Stringf(String template, Object... args)Alias forformat(String, Object...).static Stringformat(String template, Object... args)Substitutes 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.static List<String>splitOnCommas(CharSequence sequence)Convenience method that splits the given comma-delimitedCharSequence, omitting any empty strings and trimming leading and trailing whitespace.static List<String>splitToList(CharSequence sequence)Splits the givenCharSequence, using aSPACEas the separator character, omitting any empty strings and trimming leading and trailing whitespace.static List<String>splitToList(CharSequence sequence, char separator)Splits the givenCharSequence, using the specified separator character, omitting any empty strings and trimming leading and trailing whitespace.static List<String>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.static List<String>splitToList(CharSequence sequence, String separator)Splits the givenCharSequence, using the specified separator string, omitting any empty strings and trimming leading and trailing whitespace.static List<String>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.static Iterable<String>splitWithTrimAndOmitEmpty(CharSequence sequence)Splits the givenCharSequence, using aSPACEas the separator character, omitting any empty strings and trimming leading and trailing whitespace.static Iterable<String>splitWithTrimAndOmitEmpty(CharSequence sequence, char separator)Splits the givenCharSequence, using the specified separator character, omitting any empty strings and trimming leading and trailing whitespace.static Iterable<String>splitWithTrimAndOmitEmpty(CharSequence sequence, String separator)Splits the givenCharSequence, using the specified separator string, omitting any empty strings and trimming leading and trailing whitespace.
-
-
-
Field Detail
-
SPACE
public static final char SPACE
A space character.- See Also:
- Constant Field Values
-
TAB
public static final char TAB
A tab character.- See Also:
- Constant Field Values
-
COMMA
public static final char COMMA
A comma character.- See Also:
- Constant Field Values
-
NEWLINE
public static final char NEWLINE
A newline character.- See Also:
- Constant Field Values
-
-
Method Detail
-
splitWithTrimAndOmitEmpty
public static Iterable<String> splitWithTrimAndOmitEmpty(CharSequence sequence)
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:
splitWithTrimAndOmitEmpty(CharSequence, char)
-
splitWithTrimAndOmitEmpty
public static Iterable<String> splitWithTrimAndOmitEmpty(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 splitseparator- the separator character to use- Returns:
- an Iterable over the split strings
-
splitWithTrimAndOmitEmpty
public static Iterable<String> splitWithTrimAndOmitEmpty(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 splitseparator- the separator to use, e.g.", "- Returns:
- an Iterable over the split strings
-
splitToList
public static List<String> splitToList(CharSequence sequence)
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:
splitWithTrimAndOmitEmpty(CharSequence, char)
-
splitToList
public static List<String> splitToList(CharSequence sequence, char separator)
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:
splitWithTrimAndOmitEmpty(CharSequence, char)
-
splitToList
public static List<String> 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. 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
-
splitToList
public static List<String> splitToList(CharSequence sequence, String separator)
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
-
splitToList
public static List<String> 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. 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
-
splitOnCommas
public static List<String> splitOnCommas(CharSequence sequence)
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:
splitWithTrimAndOmitEmpty(CharSequence, char)
-
blankToNull
public static String blankToNull(String sequence)
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
public static String format(String template, Object... args)
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 http://stackoverflow.com/questions/12786902/performance-javas-string-format- 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
public static String f(String template, Object... args)
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
public static String formatGuavaStyle(String template, Object... args)
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:
format(String, Object...)
-
formatSlf4jJStyle
public static String formatSlf4jJStyle(String template, Object... args)
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:
format(String, Object...)
-
-