Class StringUtils


  • public class StringUtils
    extends Object
    The StringUtils class provides frequently used utility methods dealing with String values and which are not found in the java.lang.String class.

    All functions have been implemented with extreme caution in order to provide a maximum performance.

    Version:
    $Revision$ $Date$
    • Field Detail

      • DELIMITOR_SPACE

        public static String DELIMITOR_SPACE
      • DELIMITOR_UNDERSCORE

        public static String DELIMITOR_UNDERSCORE
    • Constructor Detail

      • StringUtils

        protected StringUtils()
    • Method Detail

      • split

        public static List<String> split​(String text,
                                         char[] separators,
                                         boolean trimTokens,
                                         List<String> tokens)
        Splits the given text into a list of tokens by using the supplied separators. Empty tokens are created for successive separators, or if the supplied text starts with or ends with a separator. If the given text string is empty, and empty list is returned, but never null. The tokens added to list will never contain separators.
        Parameters:
        text - the text to be split into tokens
        separators - the characters used to separate the tokens
        trimTokens - if true, white space characters are removed from both ends of each token
        tokens - can be null. If not null, all tokens are added to this list and the method it, otherwise a new list is created.
        Returns:
        a list of tokens extracted from the given text, never null
        Throws:
        IllegalArgumentException - if one of the arguments was null
        See Also:
        StringTokenizer
      • split

        public static String[] split​(String text,
                                     char[] separators,
                                     boolean trimTokens)
        Splits the given text into a list of tokens by using the supplied separators. Empty tokens are created for successive separators, or if the the supplied text starts with or ends with a separator. If the given text string is empty, and empty array is returned, but never null. The tokens in the returned array will never contain separators.
        Parameters:
        text - the text to be splitted into tokens
        separators - the characters used to separate the tokens
        trimTokens - if true, white space characters are removed from both ends of each token
        Returns:
        an array of tokens extracted from the given text, never null
        See Also:
        StringTokenizer
      • join

        public static String join​(Object[] tokens,
                                  String separator)
        Joins the given array of tokens to a new text string. The given separator string is put between each of the tokens. If a token in the array is null or empty, an empty string string is appended to the resulting text. The resulting text string will always contain tokens.length - 1 separators, if the separator is not part of one of the tokens itself.
        Parameters:
        tokens - the list of tokens to join, must not be null
        separator - the separator string, must not be null
        Returns:
        the list of tokens as a text string
        Throws:
        IllegalArgumentException - if one of the arguments was null
        See Also:
        split(String, char[], boolean)
      • join

        public static String join​(List<? extends Object> tokens,
                                  String separator)
        Joins the given array of tokens to a new text string. The given separator string is put between each of the tokens. If a token in the array is null or empty, an empty string is appended to the resulting text. The resulting text string will always contain tokens.length - 1 separators, if the separator is not part of one of the tokens itself.
        Parameters:
        tokens - the list of tokens to join, must not be null
        separator - the separator string, must not be null
        Returns:
        the list of tokens as a text string
        Throws:
        IllegalArgumentException - if one of the arguments was null
        See Also:
        join(Object[], String)
      • isIntegerString

        public static boolean isIntegerString​(String token)
        Checks whether the given token string represents an integer number or not.
        Parameters:
        token - the token string to be checked
        Returns:
        true if the string represents an integer (radix=10)
      • isIntegerString

        public static boolean isIntegerString​(String token,
                                              int radix)
        Checks whether the given token string represents an integer number or not.
        Parameters:
        token - the token string to be checked
        radix - the radix of the integer represented by the token string
        Returns:
        true if the string represents an integer with tzhe given radix
      • toIntArray

        public static int[] toIntArray​(String text,
                                       String delim)
        Converts the given text into an int array.

        The number values are expected to be separated by one of the characters given in the delimiter string.

        If the delimiter string is null or empty, the default delimiter "," will be used.

        Parameters:
        text - the text to be converted
        delim - the delimiter between the number values
        Returns:
        the int array parsed from the given text
        Throws:
        IllegalArgumentException - if the text is null or cannot be converted to an array of the requested number type
      • toFloatArray

        public static float[] toFloatArray​(String text,
                                           String delim)
        Converts the given text into an float array.

        The number values are expected to be separated by one of the characters given in the delimiter string.

        If the delimiter string is null or empty, the default delimiter "," will be used.

        Parameters:
        text - the text to be converted
        delim - the delimiter between the number values
        Returns:
        the float array parsed from the given text
        Throws:
        IllegalArgumentException - if the text is null or cannot be converted to an array of the requested number type
      • toDoubleArray

        public static double[] toDoubleArray​(String text,
                                             String delim)
        Converts the given text into an double array.

        The number values are expected to be separated by one of the characters given in the delimiter string.

        If the delimiter string is null or empty, the default delimiter "," will be used.

        Parameters:
        text - the text to be converted
        delim - the delimiter between the number values
        Returns:
        the double array parsed from the given text
        Throws:
        IllegalArgumentException - if the text is null or cannot be converted to an array of the requested number type
      • toStringArray

        public static String[] toStringArray​(String text,
                                             String delims)
        Converts the given text into an array of String tokens.

        The number values are expected to be separated by one of the characters given in the delimiter string.

        If the delimiter string is null or empty, the default delimiter "," will be used.

        Parameters:
        text - the text to be converted
        delims - the delimiter characters used between the tokens
        Returns:
        the String array parsed from the given text, never null
        Throws:
        IllegalArgumentException - if the text is null or cannot be converted to an array of the requested number type
      • toStringArray

        public static String[] toStringArray​(Object[] objArray)
        Converts the given object array into a string array. If the given object array is already an instance of a string array, it is simply type-casted and returned. Otherwise, a new string array is created and - for each non-null object - the value of object.toString() is stored, null-values remain null-values.
        Parameters:
        objArray - the object array to be converted, if null the method returns null too
        Returns:
        the string array
      • isNullOrEmpty

        public static boolean isNullOrEmpty​(String str)
        Tests whether or not the given string is null or empty.
        Parameters:
        str - the string to be tested
        Returns:
        true if so
      • isNotNullAndNotEmpty

        public static boolean isNotNullAndNotEmpty​(String str)
        Tests whether or not the given string is not null and not empty.
        Parameters:
        str - the string to be tested
        Returns:
        true if so
      • isNullOrBlank

        public static boolean isNullOrBlank​(String value)
        Tests if the input string is null, or if it contains only white spaces.
        Parameters:
        value - the input string to check
        Returns:
        true if the input string is null or blank; false otherwise.
      • addToArray

        public static String[] addToArray​(String[] array,
                                          String toAdd)
                                   throws IllegalArgumentException
        Gives a new StringArray who contains both, all Strings form the given Array and the given String. The given String was added to the end of array
        Returns:
        new String[] with all Strings
        Throws:
        IllegalArgumentException - if one of the arguments are null
      • removeFromArray

        public static String[] removeFromArray​(String[] array,
                                               String toRemove)
                                        throws IllegalArgumentException
        Gives a new StringArray who contains all Strings form the given Array excepting the given String. The first occurrence of the given String was removed.
        Returns:
        new String[] without the first occurrence of the given String
        Throws:
        IllegalArgumentException - if one of the arguments are null
      • removeFromArray

        public static String[] removeFromArray​(String[] array,
                                               String[] toRemove)
                                        throws IllegalArgumentException
        Gives a new StringArray who contains all Strings form the given Array excepting the String from the array to remove. The first occurrence of the given Strings in the string array to remove was removed.
        Returns:
        new String[] without the first occurrence of the given Strings in the string array to remove
        Throws:
        IllegalArgumentException - if array is null
      • addArrays

        public static String[] addArrays​(String[] arr1,
                                         String[] arr2)
                                  throws IllegalArgumentException
        Returns a string array which is a concatenation of the two given string arrays.
        Returns:
        a new String[] which is a concatenation of the two given string arrays.
        Throws:
        IllegalArgumentException - if one of the arguments are null
      • contains

        public static boolean contains​(String[] a,
                                       String s)
        Tests whether a given string is contained in a given string array.
        Parameters:
        a - the string array in which to search
        s - the string for which the search is performed
        Returns:
        true if the string s is contained in the array a
        Throws:
        IllegalArgumentException - if one of the arguments are null
      • containsIgnoreCase

        public static boolean containsIgnoreCase​(String[] a,
                                                 String s)
        Tests whether a given string is contained in a given string array.
        Parameters:
        a - the string array in which to search
        s - the string for which the search is performed
        Returns:
        true if the string s is contained in the array a
        Throws:
        IllegalArgumentException - if one of the arguments are null
      • startsWithIgnoreCase

        public static boolean startsWithIgnoreCase​(String inputValue,
                                                   String prefix)
        Tests if this string starts with the specified prefix, ignoring the case sensitive.
        Parameters:
        inputValue - the input string to test
        prefix - the prefix
        Returns:
        true if the input string is a prefix; false otherwise.
      • endsWithIgnoreCase

        public static boolean endsWithIgnoreCase​(String input,
                                                 String... suffixes)
        Tests if this string ends with any of the given suffixes, ignoring the case sensitive.
        Parameters:
        input - the input string to test
        suffixes - the list of suffixes
        Returns:
        true if the input string is a prefix; false otherwise.
      • containsIgnoreCase

        public static boolean containsIgnoreCase​(String input,
                                                 String value)
      • containsIgnoreCase

        public static boolean containsIgnoreCase​(List<String> l,
                                                 String s)
        Tests whether a given string is contained in a list.
        Parameters:
        l - the string list in which to search
        s - the string for which the search is performed
        Returns:
        true if the string s is contained in the array a
        Throws:
        IllegalArgumentException - if one of the arguments are null
      • areEntriesUnique

        public static boolean areEntriesUnique​(String[] array)
        Retrieves whether the entries in the string array are unique - or not.
        Parameters:
        array - the array containing the strings
        Returns:
        true if the entries in the string array are unique, otherwise false.
      • indexOf

        public static int indexOf​(String[] a,
                                  String s)
        Gets the array index of a given string in a given string array or -1 if the string could not be found.
        Parameters:
        a - the string array in which to search
        s - the string for which the search is performed
        Returns:
        the array index of the first occurence of s in a or -1 if it is not cointained in the array
        Throws:
        IllegalArgumentException - if one of the arguments is null
      • indexOfIgnoreCase

        public static int indexOfIgnoreCase​(String[] a,
                                            String s)
        Gets the array index of a given string in a given string array or -1 if the string could not be found.
        Parameters:
        a - the string array in which to search
        s - the string for which the search is performed
        Returns:
        the array index of the first occurrence of s in a or -1 if it is not contained in the array
        Throws:
        IllegalArgumentException - if one of the arguments is null
      • indexOfIgnoreCase

        public static int indexOfIgnoreCase​(List<String> l,
                                            String s)
        Gets the list index of a given string in a given string list or -1 if the string could not be found.
        Parameters:
        l - the string list in which to search
        s - the string for which the search is performed
        Returns:
        the array index of the first occurrence of s in a or -1 if it is not contained in the array
        Throws:
        IllegalArgumentException - if one of the arguments is null
      • arrayToCsv

        public static String arrayToCsv​(Object array)
        Gets a comma separate value string for the given array object.
        Parameters:
        array - the array object of values.
        Returns:
        a comma separate value string for the given array object.
        Throws:
        IllegalArgumentException - if the given Object is not an array or null.
      • arrayToString

        public static String arrayToString​(Object array,
                                           String s)
        Converts an array into a string.
        Parameters:
        array - the array object
        s - the separator string, e.g. ","
        Returns:
        a string represenation of the array
        Throws:
        IllegalArgumentException - if the given Object is not an array or null.
      • csvToArray

        public static String[] csvToArray​(String csvString)
        Gets a String[] from the given comma separated value string.
        Parameters:
        csvString - the CSV (comma separated value) String.
        Returns:
        an array of strings created from the given comma separated value string, never null
        Throws:
        IllegalArgumentException - if the given csvString is null or empty.
      • stringToArray

        public static String[] stringToArray​(String csvString,
                                             String delim)
        Gets a String[] from the given comma separated value string given a delimiter.
        Parameters:
        csvString - the delimited String.
        delim - the separator string, e.g. ","
        Returns:
        an array of strings created from the given comma separated value string, never null
        Throws:
        IllegalArgumentException - if the given Object is not an array or null.
      • formatColor

        public static String formatColor​(Color c)
        Returns a string representation of the given color value.
      • createValidName

        public static String createValidName​(String name,
                                             char[] validChars,
                                             char replaceChar)
        Creates a valid name for the given source name. The method returns a string which is the given name where each occurence of a character which is not a letter, a digit or one of the given valid characters is replaced by the given replace character. The returned string always has the same length as the source name.
        Parameters:
        name - the source name, must not be null
        validChars - the array of valid characters
        replaceChar - the replace character
      • replaceWord

        public static String replaceWord​(String string,
                                         String oldWord,
                                         String newWord)
        Replaces all occurrences of the old word within the given string by the new word.
        Parameters:
        string - the string within all occurrences of the old word are to be replaced.
        oldWord - the word to be replaced. Must not be null and must not be empty.
        newWord - the new word. Must not be null.
        Returns:
        the resulting string, never null.
      • isIdentifier

        public static boolean isIdentifier​(String s)
        Tests whether or not the given string is valid identifier. Valid identifiers have a length greater than zero, start with a letter or underscore followed by letters, digits or underscores.
        Parameters:
        s - the string to test
        Returns:
        true if the s is a valid node ifentifier, false otherwise
      • isNumeric

        public static boolean isNumeric​(String str,
                                        Class<? extends Number> clazz)
        Checks if the string is numeric
        Parameters:
        str - the String input
        clazz - the type to check against
        Returns:
        true if numeric false if not
      • firstLetterUp

        public static String firstLetterUp​(String string)
        Turns the first letter of the given string to upper case.
        Parameters:
        string - the string to change
        Returns:
        a changed string
      • indexOfSpecificOccurrence

        public static int indexOfSpecificOccurrence​(String a,
                                                    String s,
                                                    int i)
        Gets the array index of the i-th occurrence of a given string in a string array or -1 if the string could not be found.
        Parameters:
        a - the string array in which to search
        s - the string for which the search is performed
        i - the index of the occurrence of the requested string
        Returns:
        the array index of the i-th occurrence of s in a or -1 if s is contained less than i times in the array or if i is less than 1
        Throws:
        IllegalArgumentException - if a s are null
      • padNum

        public static String padNum​(int num,
                                    int max,
                                    char c)
        Adds padding to an integer 1 becomes 001 or __1
        Parameters:
        num - the integer value
        max - the desired string length
        c - the inserted character
        Returns:
        padded number as string
      • makeStringsUnique

        public static String[] makeStringsUnique​(String[] strings)
        Makes the strings in the given array unique. Strings which occur more than once in the array get an appendix with the index of its occurrence. Strings that only differ in their capitalization will be treated as having the same string value, but in the output they will keep their respective capitalization Example:
             'name', 'duplicate', 'other', 'duplicate', 'case_duplicate', 'CASE_DUPLICATE'
         --> 'name', 'duplicate_1', 'other', 'duplicate_2', 'case_duplicate_1', 'CASE_DUPLICATE_2'
         
        Parameters:
        strings - the strings to make unique
        Returns:
        a new array with the changed names
      • toXMLString

        public static String toXMLString​(org.jdom2.Element element)
      • toXMLString

        public static String toXMLString​(org.jdom2.output.Format format,
                                         org.jdom2.Element element)
      • toXMLString

        public static String toXMLString​(org.jdom2.Document element)
      • toXMLString

        public static String toXMLString​(org.jdom2.output.Format format,
                                         org.jdom2.Document element)
      • getStringCaseVariations

        public static String[] getStringCaseVariations​(String s)