Class KiwiStrings


  • public final class KiwiStrings
    extends Object
    Utility methods relating to strings or similar.
    • Constructor Detail

      • KiwiStrings

        public KiwiStrings()
    • Method Detail

      • splitWithTrimAndOmitEmpty

        public static Iterable<String> splitWithTrimAndOmitEmpty​(CharSequence sequence,
                                                                 char separator)
        Splits the given CharSequence, using the specified separator character, omitting any empty strings and trimming leading and trailing whitespace.
        Parameters:
        sequence - the character sequence to be split
        separator - the separator character to use
        Returns:
        an Iterable over the split strings
      • splitWithTrimAndOmitEmpty

        public static Iterable<String> splitWithTrimAndOmitEmpty​(CharSequence sequence,
                                                                 String separator)
        Splits the given CharSequence, using the specified separator string, omitting any empty strings and trimming leading and trailing whitespace.
        Parameters:
        sequence - the character sequence to be split
        separator - the separator to use, e.g. ", "
        Returns:
        an Iterable over the split strings
      • splitToList

        public static List<String> splitToList​(CharSequence sequence)
        Splits the given CharSequence, using a SPACE as 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 given CharSequence, 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
        separator - 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 given CharSequence, 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
        separator - the separator character to use
        maxGroups - 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 given CharSequence, 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
        separator - 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 given CharSequence, 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
        separator - the separator string to use
        maxGroups - 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-delimited CharSequence, 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)
      • nullSafeSplitOnCommas

        public static List<String> nullSafeSplitOnCommas​(CharSequence sequence)
        Convenience method that splits the given comma-delimited CharSequence, 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 sequence is blank
        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 sequence is blank, otherwise return sequence
      • format

        public static String format​(String template,
                                    Object... args)
        Substitutes each %s or {} in template with an argument. These are matched by position: the first %s (or {}) gets args[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 %s or {} 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 %s placeholders 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 %s placeholders.

        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 the String.format(java.util.Locale, String, Object...) method. A slight modification we made is to not re-assign the template argument. Guava 25.1 moved this very useful functionality into the Guava Strings class as {Strings.lenientFormat(String, Object...)}. However, it only accepts %s as the replacement placeholder. For performance comparisons of the JDK String.format(String, Object...) method, see http://stackoverflow.com/questions/12786902/performance-javas-string-format

        Parameters:
        template - a non-null string containing 0 or more %s or {} placeholders.
        args - the arguments to be substituted into the message template. Arguments are converted to strings using String.valueOf(Object). Arguments can be null.
        Returns:
        the formatted string after making replacements
      • f

        public static String f​(String template,
                               Object... args)
        Parameters:
        template - a non-null string containing 0 or more %s or {} placeholders.
        args - the arguments to be substituted into the message template. Arguments are converted to strings using String.valueOf(Object). Arguments can be null.
        Returns:
        the formatted string after making replacements
      • formatGuavaStyle

        public static String formatGuavaStyle​(String template,
                                              Object... args)
        Same as format(String, Object...) assuming Guava-style placeholders.
        Parameters:
        template - a non-null string containing 0 or more %s placeholders.
        args - the arguments to be substituted into the message template. Arguments are converted to strings using String.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 as format(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 using String.valueOf(Object). Arguments can be null.
        Returns:
        the formatted string after making replacements
        See Also:
        format(String, Object...)