Class StringUtil


  • public class StringUtil
    extends java.lang.Object
    Various String utility functions. Most of the functions herein are re-implementations of the ones in apache commons StringUtils. The reason for re-implementing this is that the functions are fairly simple and using my own implementation saves the inclusion of a 200Kb jar file.
    • Constructor Summary

      Constructors 
      Constructor Description
      StringUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String collapsePathDots​(java.lang.String path)
      Changes a path containing '..', '.' and empty dirs into a path that doesn't.
      static java.lang.String defaultIfNull​(java.lang.String text)
      If the given text is null return "", the original text otherwise.
      static java.lang.String defaultIfNull​(java.lang.String text, java.lang.String defaultValue)
      If the given text is null return "", the given defaultValue otherwise.
      static boolean endsWithIgnoreCase​(java.lang.String source, java.lang.String suffix)
      Whether the given source string ends with the given suffix, ignoring case.
      static boolean equals​(java.lang.String text1, java.lang.String text2)
      Null-safe string comparator
      static int hashCode​(java.lang.String... values)  
      static boolean isBlank​(java.lang.String text)
      Whether the String is null, zero-length and does contain only whitespace.
      static boolean isEmpty​(java.lang.String text)
      Whether the given string is null or zero-length.
      static boolean isNotBlank​(java.lang.String text)
      Whether the String is not null, not zero-length and does not contain of only whitespace.
      static java.lang.String substringAfter​(java.lang.String text, char c)
      Gives the substring of the given text after the given separator.
      static java.lang.String substringAfterLast​(java.lang.String text, char separator)
      Gives the substring of the given text after the last occurrence of the given separator.
      static java.lang.String substringBefore​(java.lang.String text, char separator)
      Gives the substring of the given text before the given separator.
      static java.lang.String substringBeforeLast​(java.lang.String text, char separator)
      Gives the substring of the given text before the last occurrence of the given separator.
      static java.lang.String toString​(java.lang.Object... keyValues)
      Pretty toString printer.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • StringUtil

        public StringUtil()
    • Method Detail

      • collapsePathDots

        public static java.lang.String collapsePathDots​(java.lang.String path)
        Changes a path containing '..', '.' and empty dirs into a path that doesn't. X/foo/../Y is changed into 'X/Y', etc. Does not handle invalid paths like "../".
        Parameters:
        path -
        Returns:
        the normalized path
      • isNotBlank

        public static boolean isNotBlank​(java.lang.String text)
        Whether the String is not null, not zero-length and does not contain of only whitespace.
        Parameters:
        text -
        Returns:
        Whether the String is not null, not zero-length and does not contain of
      • isBlank

        public static boolean isBlank​(java.lang.String text)
        Whether the String is null, zero-length and does contain only whitespace.
        Returns:
        Whether the String is null, zero-length and does contain only whitespace.
      • isEmpty

        public static boolean isEmpty​(java.lang.String text)
        Whether the given string is null or zero-length.
        Parameters:
        text - the input for this method
        Returns:
        Whether the given string is null or zero-length.
      • endsWithIgnoreCase

        public static boolean endsWithIgnoreCase​(java.lang.String source,
                                                 java.lang.String suffix)
        Whether the given source string ends with the given suffix, ignoring case.
        Parameters:
        source -
        suffix -
        Returns:
        Whether the given source string ends with the given suffix, ignoring case.
      • defaultIfNull

        public static java.lang.String defaultIfNull​(java.lang.String text)
        If the given text is null return "", the original text otherwise.
        Parameters:
        text -
        Returns:
        If the given text is null "", the original text otherwise.
      • defaultIfNull

        public static java.lang.String defaultIfNull​(java.lang.String text,
                                                     java.lang.String defaultValue)
        If the given text is null return "", the given defaultValue otherwise.
        Parameters:
        text -
        defaultValue -
        Returns:
        If the given text is null "", the given defaultValue otherwise.
      • equals

        public static boolean equals​(java.lang.String text1,
                                     java.lang.String text2)
        Null-safe string comparator
        Parameters:
        text1 -
        text2 -
        Returns:
        whether the two strings are equal
      • toString

        public static java.lang.String toString​(java.lang.Object... keyValues)
        Pretty toString printer.
        Parameters:
        keyValues -
        Returns:
        a string representation of the input values
      • hashCode

        public static int hashCode​(java.lang.String... values)
      • substringBefore

        public static java.lang.String substringBefore​(java.lang.String text,
                                                       char separator)
        Gives the substring of the given text before the given separator. If the text does not contain the given separator then the given text is returned.
        Parameters:
        text -
        separator -
        Returns:
        the substring of the given text before the given separator.
      • substringBeforeLast

        public static java.lang.String substringBeforeLast​(java.lang.String text,
                                                           char separator)
        Gives the substring of the given text before the last occurrence of the given separator. If the text does not contain the given separator then the given text is returned.
        Parameters:
        text -
        separator -
        Returns:
        the substring of the given text before the last occurrence of the given separator.
      • substringAfterLast

        public static java.lang.String substringAfterLast​(java.lang.String text,
                                                          char separator)
        Gives the substring of the given text after the last occurrence of the given separator. If the text does not contain the given separator then "" is returned.
        Parameters:
        text -
        separator -
        Returns:
        the substring of the given text after the last occurrence of the given separator.
      • substringAfter

        public static java.lang.String substringAfter​(java.lang.String text,
                                                      char c)
        Gives the substring of the given text after the given separator. If the text does not contain the given separator then "" is returned.
        Parameters:
        text - the input text
        c - the separator char
        Returns:
        the substring of the given text after the given separator.