Class StringUtils


  • public class StringUtils
    extends Object
    Utilities for String manipulation.
    • Constructor Detail

      • StringUtils

        public StringUtils()
    • Method Detail

      • getMessageFromRootCauseOfThrowable

        public static String getMessageFromRootCauseOfThrowable​(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 String getStringFromStackTrace​(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 File getFileForFilename​(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 JarFile getJarFileForFilename​(String filename)
                                             throws 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:
        IOException - if there is a problem opening the JarFile
      • getDecodedStringFromURLString

        public static String getDecodedStringFromURLString​(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 String getEncodedURLStringFromString​(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 String replaceAll​(String theString,
                                        String toReplace,
                                        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​(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​(String str1,
                                              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 String leftAlignedPaddedString​(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 String rightAlignedPaddedString​(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 String[] split​(String valuesString,
                                     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 Set<String> convertCommaSeparatedStringToSet​(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 String toJVMIDString​(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 String booleanArrayToString​(boolean[] ba)
        Utility to convert a boolean[] to a String.
        Parameters:
        ba - The boolean[]
        Returns:
        String version
      • intArrayToString

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

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

        public static String collectionToString​(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 String getNameOfClass​(Class cls)
      • mapToString

        public static String mapToString​(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​(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​(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 String exponentialFormatBigDecimal​(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 String removeSpecialTagsFromString​(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 String getStringFromException​(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