Class StringUtils


  • public class StringUtils
    extends java.lang.Object
    Utilities for String manipulation.
    • Constructor Summary

      Constructors 
      Constructor Description
      StringUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean areStringsEqual​(java.lang.String str1, java.lang.String str2)
      Utility to tell if two strings are the same.
      static java.lang.String booleanArrayToString​(boolean[] ba)
      Utility to convert a boolean[] to a String.
      static java.lang.String collectionToString​(java.util.Collection coll)
      Converts the given collection of objects to string as a comma-separated list.
      static java.util.Set<java.lang.String> convertCommaSeparatedStringToSet​(java.lang.String str)
      Converts the input comma-separated string into a Set of the individual strings, with each string trimmed and converted to UPPERCASE.
      static java.lang.String exponentialFormatBigDecimal​(java.math.BigDecimal bd)
      Formats the given BigDecimal value into a floating-point literal (like we find in SQL).
      static java.lang.String getDecodedStringFromURLString​(java.lang.String urlString)
      Convenience method to decode a URL string for use (so spaces are allowed)
      static java.lang.String getEncodedURLStringFromString​(java.lang.String string)
      Convenience method to encode a URL string for use (so spaces are allowed)
      static java.io.File getFileForFilename​(java.lang.String filename)
      Convenience method to get a File for the specified filename.
      static java.util.jar.JarFile getJarFileForFilename​(java.lang.String filename)
      Convenience method to get a JarFile for the specified filename.
      static java.lang.String getMessageFromRootCauseOfThrowable​(java.lang.Throwable thr)
      Convenience method to take a throwable and navigate up to the root cause and return the message of the root cause.
      static java.lang.String getNameOfClass​(java.lang.Class cls)  
      static java.lang.String getStringFromException​(java.lang.Throwable exception)
      Gets a String message from an Exception.
      static java.lang.String getStringFromStackTrace​(java.lang.Throwable ex)
      Convert an exception to a String with full stack trace
      static java.lang.String intArrayToString​(int[] ia)
      Utility to convert an int[] to a String.
      static boolean isEmpty​(java.lang.String s)
      check string is null or length is 0.
      static boolean isWhitespace​(java.lang.String str)
      Utility to check if a string is whitespace.
      static java.lang.String leftAlignedPaddedString​(java.lang.String input, int length)
      Utility to return a left-aligned version of a string padded to the number of characters specified.
      static java.lang.String mapToString​(java.util.Map map)
      Converts the given map of objects to string as a comma-separated list.
      static boolean notEmpty​(java.lang.String s)
      check string is not null and length > 0.
      static java.lang.String objectArrayToString​(java.lang.Object[] arr)
      Utility to convert an Object[] to a String.
      static java.lang.String removeSpecialTagsFromString​(java.lang.String str)
      Method to return the input string with all special tags (end-of-line, tab, etc) replaced by spaces.
      static java.lang.String replaceAll​(java.lang.String theString, java.lang.String toReplace, java.lang.String replacement)
      A more efficient version than String.replace(CharSequence, CharSequence) which uses Regex for the implementation and requires compilation for every execution.
      static java.lang.String rightAlignedPaddedString​(java.lang.String input, int length)
      Utility to return a right-aligned version of a string padded to the number of characters specified.
      static java.lang.String[] split​(java.lang.String valuesString, java.lang.String token)
      Splits a list of values separated by a token
      static java.lang.String toJVMIDString​(java.lang.Object obj)
      Utility to convert an object to a JVM type string.
      • Methods inherited from class java.lang.Object

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

      • StringUtils

        public StringUtils()
    • Method Detail

      • getMessageFromRootCauseOfThrowable

        public static java.lang.String getMessageFromRootCauseOfThrowable​(java.lang.Throwable thr)
        Convenience method to take a throwable and navigate up to the root cause and return the message of the root cause.
        Parameters:
        thr - The Throwable
        Returns:
        The message from the root cause
      • getStringFromStackTrace

        public static java.lang.String getStringFromStackTrace​(java.lang.Throwable ex)
        Convert an exception to a String with full stack trace
        Parameters:
        ex - the exception
        Returns:
        a String with the full stacktrace error text
      • getFileForFilename

        public static java.io.File getFileForFilename​(java.lang.String filename)
        Convenience method to get a File for the specified filename. Caters for URL-encoded characters in the filename (treatment of spaces on Windows etc)
        Parameters:
        filename - Name of file
        Returns:
        The File
      • getJarFileForFilename

        public static java.util.jar.JarFile getJarFileForFilename​(java.lang.String filename)
                                                           throws java.io.IOException
        Convenience method to get a JarFile for the specified filename. Caters for URL-encoded characters in the filename (treatment of spaces on Windows etc)
        Parameters:
        filename - Name of file
        Returns:
        The JarFile
        Throws:
        java.io.IOException - if there is a problem opening the JarFile
      • getDecodedStringFromURLString

        public static java.lang.String getDecodedStringFromURLString​(java.lang.String urlString)
        Convenience method to decode a URL string for use (so spaces are allowed)
        Parameters:
        urlString - The URL string
        Returns:
        The string
      • getEncodedURLStringFromString

        public static java.lang.String getEncodedURLStringFromString​(java.lang.String string)
        Convenience method to encode a URL string for use (so spaces are allowed)
        Parameters:
        string - The string
        Returns:
        The encoded string
      • replaceAll

        public static java.lang.String replaceAll​(java.lang.String theString,
                                                  java.lang.String toReplace,
                                                  java.lang.String replacement)
        A more efficient version than String.replace(CharSequence, CharSequence) which uses Regex for the implementation and requires compilation for every execution.
        Parameters:
        theString - The string to use
        toReplace - The string to replace.
        replacement - The replacement string.
        Returns:
        The updated string after replacing.
      • isWhitespace

        public static boolean isWhitespace​(java.lang.String str)
        Utility to check if a string is whitespace. If the string is null, returns true also.
        Parameters:
        str - The string to check
        Returns:
        Whether the string is just whitespace
      • areStringsEqual

        public static boolean areStringsEqual​(java.lang.String str1,
                                              java.lang.String str2)
        Utility to tell if two strings are the same. Extends the basic String 'equals' method by allowing for nulls.
        Parameters:
        str1 - The first string
        str2 - The second string
        Returns:
        Whether the strings are equal.
      • leftAlignedPaddedString

        public static java.lang.String leftAlignedPaddedString​(java.lang.String input,
                                                               int length)
        Utility to return a left-aligned version of a string padded to the number of characters specified.
        Parameters:
        input - The input string
        length - The length desired
        Returns:
        The updated string
      • rightAlignedPaddedString

        public static java.lang.String rightAlignedPaddedString​(java.lang.String input,
                                                                int length)
        Utility to return a right-aligned version of a string padded to the number of characters specified.
        Parameters:
        input - The input string
        length - The length desired
        Returns:
        The updated string
      • split

        public static java.lang.String[] split​(java.lang.String valuesString,
                                               java.lang.String token)
        Splits a list of values separated by a token
        Parameters:
        valuesString - the text to be splited
        token - the token
        Returns:
        an array with all values
      • convertCommaSeparatedStringToSet

        public static java.util.Set<java.lang.String> convertCommaSeparatedStringToSet​(java.lang.String str)
        Converts the input comma-separated string into a Set of the individual strings, with each string trimmed and converted to UPPERCASE.
        Parameters:
        str - The input string
        Returns:
        Set of the comma-separated strings that it is comprised of
      • toJVMIDString

        public static java.lang.String toJVMIDString​(java.lang.Object obj)
        Utility to convert an object to a JVM type string. Returns the same as would have been output from Object.toString() if the class hadn't overridden it.
        Parameters:
        obj - The object
        Returns:
        The String version
      • booleanArrayToString

        public static java.lang.String booleanArrayToString​(boolean[] ba)
        Utility to convert a boolean[] to a String.
        Parameters:
        ba - The boolean[]
        Returns:
        String version
      • intArrayToString

        public static java.lang.String intArrayToString​(int[] ia)
        Utility to convert an int[] to a String.
        Parameters:
        ia - The int[]
        Returns:
        String version
      • objectArrayToString

        public static java.lang.String objectArrayToString​(java.lang.Object[] arr)
        Utility to convert an Object[] to a String.
        Parameters:
        arr - The Object[]
        Returns:
        String version
      • collectionToString

        public static java.lang.String collectionToString​(java.util.Collection coll)
        Converts the given collection of objects to string as a comma-separated list. If the list is empty the string "<none>" is returned.
        Parameters:
        coll - collection of objects to be converted
        Returns:
        A string containing each object in the given collection, converted toString() and separated by commas.
      • getNameOfClass

        public static java.lang.String getNameOfClass​(java.lang.Class cls)
      • mapToString

        public static java.lang.String mapToString​(java.util.Map map)
        Converts the given map of objects to string as a comma-separated list. If the map is empty the string "<none>" is returned.
        Parameters:
        map - Map to be converted
        Returns:
        A string containing each object in the given map, converted toString() and separated by commas.
      • isEmpty

        public static boolean isEmpty​(java.lang.String s)
        check string is null or length is 0.
        Parameters:
        s - check string
        Returns:
        return true if string is null or length is 0. return false other case.
      • notEmpty

        public static boolean notEmpty​(java.lang.String s)
        check string is not null and length > 0.
        Parameters:
        s - check string
        Returns:
        return true if string isnot null and length greater than 0. return false other case.
      • exponentialFormatBigDecimal

        public static java.lang.String exponentialFormatBigDecimal​(java.math.BigDecimal bd)
        Formats the given BigDecimal value into a floating-point literal (like we find in SQL). BigDecimal.toString() is not well suited to this purpose because it never uses E-notation, which causes some values with large exponents to be output as long strings with tons of zeroes in them.
        Parameters:
        bd - The number to format.
        Returns:
        The formatted String.
      • removeSpecialTagsFromString

        public static java.lang.String removeSpecialTagsFromString​(java.lang.String str)
        Method to return the input string with all special tags (end-of-line, tab, etc) replaced by spaces.
        Parameters:
        str - The string
        Returns:
        The cleaned up string
      • getStringFromException

        public static java.lang.String getStringFromException​(java.lang.Throwable exception)
        Gets a String message from an Exception. This method transforms nested exceptions into printable messages.
        Parameters:
        exception - to be read and transformed into a message to print
        Returns:
        the message to output