Class StringUtil


  • public final class StringUtil
    extends Object
    This is a common place to put String utility methods.
    • Method Detail

      • join

        public static String join​(Collection<String> strings,
                                  String delimiter)
        Join string pieces and separate with a delimiter. Similar to the perl function of the same name. If strings or delimiter are null, null is returned. Otherwise, at least an empty string will be returned.
        Parameters:
        strings - String pieces to join
        delimiter - Delimiter to put between string pieces
        Returns:
        One merged string
        See Also:
        split(java.lang.String, java.lang.String)
      • toString

        public static String toString​(Object[] array,
                                      String delim)
        Return a stringified version of the array.
        Parameters:
        array - the array
        delim - the delimiter to use between array components
        Returns:
        the string form of the array
      • toString

        public static String toString​(Object[] array,
                                      String delim,
                                      boolean includeBrackets)
        Return a stringified version of the array.
        Parameters:
        array - the array
        delim - the delimiter to use between array components
        Returns:
        the string form of the array
      • toString

        public static String toString​(Object[] array)
        Return a stringified version of the array, using a ',' as a delimiter
        Parameters:
        array - the array
        Returns:
        the string form of the array
        See Also:
        toString(Object[], String)
      • replace

        public static String replace​(String source,
                                     String search,
                                     String replace)
        Replace a single occurrence of the search string with the replace string in the source string. If any of the strings is null or the search string is zero length, the source string is returned.
        Parameters:
        source - the source string whose contents will be altered
        search - the string to search for in source
        replace - the string to substitute for search if present
        Returns:
        source string with the *first* occurrence of the search string replaced with the replace string
      • replaceAll

        public static String replaceAll​(String source,
                                        String search,
                                        String replace)
        Replace all occurrences of the search string with the replace string in the source string. If any of the strings is null or the search string is zero length, the source string is returned.
        Parameters:
        source - the source string whose contents will be altered
        search - the string to search for in source
        replace - the string to substitute for search if present
        Returns:
        source string with *all* occurrences of the search string replaced with the replace string
      • getTokens

        public static List<String> getTokens​(String str,
                                             String delimiter)
        Return the tokens in a string in a list. This is particularly helpful if the tokens need to be processed in reverse order. In that case, a list iterator can be acquired from the list for reverse order traversal.
        Parameters:
        str - String to be tokenized
        delimiter - Characters which are delimit tokens
        Returns:
        List of string tokens contained in the tokenized string
      • getLastToken

        public static String getLastToken​(String str,
                                          String delimiter)
        Return the last token in the string.
        Parameters:
        str - String to be tokenized
        delimiter - Characters which are delimit tokens
        Returns:
        the last token contained in the tokenized string
      • getFirstToken

        public static String getFirstToken​(String str,
                                           String delimiter)
        Return the first token in the string.
        Parameters:
        str - String to be tokenized
        delimiter - Characters which are delimit tokens
        Returns:
        the first token contained in the tokenized string
      • isEmpty

        public static boolean isEmpty​(String text)

        Returns whether the specified text is either empty or null.

        Parameters:
        text - The text to check; may be null;
        Returns:
        True if the specified text is either empty or null.
        Since:
        4.0
      • indexOfIgnoreCase

        public static int indexOfIgnoreCase​(String text,
                                            String str)
        Returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that:
         this.startsWith(str, k)
         
        is true.
        Parameters:
        text - any string.
        str - any string.
        Returns:
        if the str argument occurs as a substring within text, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned. If the text or str argument is null or empty then -1 is returned.
      • startsWithIgnoreCase

        public static boolean startsWithIgnoreCase​(String text,
                                                   String prefix)
        Tests if the string starts with the specified prefix.
        Parameters:
        text - the string to test.
        prefix - the prefix.
        Returns:
        true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise. Note also that true will be returned if the prefix is an empty string or is equal to the text String object as determined by the Object.equals(Object) method. If the text or prefix argument is null false is returned.
        Since:
        JDK1. 0
      • endsWithIgnoreCase

        public static boolean endsWithIgnoreCase​(String text,
                                                 String suffix)
        Tests if the string ends with the specified suffix.
      • isLetter

        public static boolean isLetter​(char c)
      • isDigit

        public static boolean isDigit​(char c)
      • isLetterOrDigit

        public static boolean isLetterOrDigit​(char c)
      • valueOf

        public static <T> T valueOf​(String value,
                                    Class type)
        Convert the given value to specified type.
        Parameters:
        value -
        type -
        Returns:
      • equalsIgnoreCase

        public static boolean equalsIgnoreCase​(String s1,
                                               String s2)
      • caseInsensitiveValueOf

        public static <T extends Enum<T>> T caseInsensitiveValueOf​(Class<T> enumType,
                                                                   String name)
      • unescape

        public static String unescape​(CharSequence string,
                                      int quoteChar,
                                      boolean useAsciiEscapes,
                                      StringBuilder sb)
        Unescape the given string
        Parameters:
        string -
        quoteChar -
        useAsciiEscapes -
        sb - a scratch buffer to use
        Returns: