Class StringUtil


  • public final class StringUtil
    extends Object
    Provides general purpose String utilities.
    • Method Detail

      • isNullOrEmpty

        public static boolean isNullOrEmpty​(String value)
        Test if a string is null if it is an empty string when trimmed.
        Parameters:
        value -
        Returns:
        null or trimmed blank string
      • nonNull

        public static String nonNull​(String value)
        Return an empty string when null, otherwise return the string.
        Parameters:
        value -
        Returns:
        string or empty string when null
      • trim

        public static String trim​(String value)
        Return an empty string when null, otherwise return the string.
        Parameters:
        value -
        Returns:
        string or empty string when null
      • getBefore

        public static String getBefore​(String value,
                                       String match)
        Returns the string within this string of the first occurrence before the first specified substring match e.g. supplying something@another.com would return something when a match of @ was supplied. If the match is not found the original supplied value is returned.
        Parameters:
        value -
        match -
        Returns:
        the string before the supplied match or the original string if no match is found
      • getBeforeLast

        public static String getBeforeLast​(String value,
                                           String match)
        Returns the string within this string of the first occurrence before the last specified substring match e.g. supplying something@another.com would return something when a match of @ was supplied. If the match is not found the original supplied value is returned.
        Parameters:
        value -
        match -
        Returns:
        the string before the supplied match or the original string if no match is found
      • getAfterLast

        public static String getAfterLast​(String value,
                                          String match)
        Returns the string within this string of the first occurrence after the last specified substring match e.g. supplying something@another.com would return another.com when a match of @ was supplied. If the match is not found the original supplied value is returned.
        Parameters:
        value -
        match -
        Returns:
        the string after the supplied match or the original string if no match is found
      • getAfter

        public static String getAfter​(String value,
                                      String match)
        Returns the string within this string of the first occurrence after the first specified substring match e.g. supplying something@another.com would return another.com when a match of @ was supplied. If the match is not found the original supplied value is returned.
        Parameters:
        value -
        match -
        Returns:
        the string after the supplied match or the original string if no match is found
      • toDefaultArray

        public static String[] toDefaultArray​(String value)
        Tokenize the supplied value, splitting it when the ! delimiter is encountered. Note that if the value is null, it does not throw an exception and instead returns an empty Array.
        Parameters:
        value - a string to be parsed
        Returns:
        the array built from the delimited value
      • toArray

        public static String[] toArray​(String value,
                                       char delimiter)
        Tokenize the supplied value, splitting it when the supplied delimiter is encountered. Note that if the value is null, it does not throw an exception and instead returns an empty Collection.
        Parameters:
        value - a string to be parsed
        delimiter - the delimiter
        Returns:
        the collection built from the delimited value
      • toDefaultList

        public static List<String> toDefaultList​(String value)
        Tokenize the supplied value, splitting it when the ! delimiter is encountered. Note that if the value is null, it does not throw an exception and instead returns an empty Collection.
        Parameters:
        value - a string to be parsed
        Returns:
        the collection built from the delimited value
      • toList

        public static List<String> toList​(String value,
                                          char delimiter)
        Tokenize the supplied value, splitting it when the supplied delimiter is encountered. Note that if the value is null, it does not throw an exception and instead returns an empty Collection.
        Parameters:
        value - a string to be parsed
        delimiter - the delimiter
        Returns:
        the collection built from the delimited value
      • toDefaultString

        public static String toDefaultString​(String... values)
      • toDefaultString

        public static String toDefaultString​(Collection<String> values)
        Convert a collection of string values into a bang (!) delimited string.
        Parameters:
        values -
        Returns:
        bang separated string
      • toString

        public static String toString​(Collection<String> values,
                                      String delimiter)
        Convert a collection of string values into a delimited string.
        Parameters:
        values -
        delimiter - delimiter string
        Returns:
        delimited string
      • equals

        public static boolean equals​(Collection<String> asCollection,
                                     String[] values)
        Compare a collection of strings to an array of strings.
      • convertByteToString

        public static final String convertByteToString​(byte[] objectGuid)
      • upperCamelCase

        public static String upperCamelCase​(String value)
        Convert a space separated string to upper camel case.
        Parameters:
        value - value
        Returns:
        upper camel cased string
      • getURLFilename

        public static String getURLFilename​(URL url)
      • compactClasspathString

        public static String compactClasspathString​(String classpathString)
        Compact a classpath string by replacing any occurence of the current working directory with '.'. If any of the remaining paths contain the user homes directory, that will be replaced with a '~'.
        Parameters:
        text - text
        Returns:
        replaced text
      • isArrayNullOrEmpty

        public static boolean isArrayNullOrEmpty​(String[] values)
      • isNullOrEmptyOrFirstElementBlank

        public static boolean isNullOrEmptyOrFirstElementBlank​(String[] values)
      • getValue

        public static String getValue​(String[] values)
      • trimToLength

        public static String trimToLength​(String text,
                                          int length)
      • getFirstItemOrNull

        public static String getFirstItemOrNull​(List<String> items)
      • capitalisedToKey

        public static String capitalisedToKey​(String name)
      • toList

        public static List<String> toList​(String value,
                                          String delimiter)
        Splits a String separated by a delimiter into tokens and returns them in a List.
        Parameters:
        value -
        delimiter -
        Returns:
        list containing tokens