Class StringUtils


  • public class StringUtils
    extends java.lang.Object
    Ripped form commons StringUtil, unless specified:

    Operations on String that are null safe.

    • IsEmpty/IsBlank - checks if a String contains text
    • Trim/Strip - removes leading and trailing whitespace
    • Equals - compares two strings null-safe
    • IndexOf/LastIndexOf/Contains - null-safe index-of checks
    • IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - index-of any of a set of Strings
    • ContainsOnly/ContainsNone - does String contains only/none of these characters
    • Substring/Left/Right/Mid - null-safe substring extractions
    • SubstringBefore/SubstringAfter/SubstringBetween - substring extraction relative to other strings
    • Split/Join - splits a String into an array of substrings and vice versa
    • Remove/Delete - removes part of a String
    • Replace/Overlay - Searches a String and replaces one String with another
    • Chomp/Chop - removes the last part of a String
    • LeftPad/RightPad/Center/Repeat - pads a String
    • UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - changes the case of a String
    • CountMatches - counts the number of occurrences of one String in another
    • IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - checks the characters in a String
    • DefaultString - protects against a null input String
    • Reverse/ReverseDelimited - reverses a String
    • Abbreviate - abbreviates a string using ellipsis
    • Difference - compares two Strings and reports on their differences
    • LevensteinDistance - the number of changes needed to change one String into another

    The StringUtils class defines certain words related to String handling.

    • null - null
    • empty - a zero-length string ("")
    • space - the space character (' ', char 32)
    • whitespace - the characters defined by Character.isWhitespace(char)
    • trim - the characters <= 32 as in String.trim()

    StringUtils handles null input Strings quietly. That is to say that a null input will return null. Where a boolean or int is being returned details vary by method.

    A side effect of the null handling is that a NullPointerException should be considered a bug in StringUtils (except for deprecated methods).

    Methods in this class give sample code to explain their operation. The symbol * is used to indicate any input including null.

    Since:
    1.0
    Version:
    $Id$
    See Also:
    String
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String EMPTY
      The empty String "".
      static java.lang.String[] EMPTY_STRING_ARRAY
      An empty immutable String array.
      static int INDEX_NOT_FOUND
      Represents a failed index search.
    • Constructor Summary

      Constructors 
      Constructor Description
      StringUtils()
      StringUtils instances should NOT be constructed in standard programming.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String bytesToHex​(byte[] bytes)  
      static java.lang.String cleanPath​(java.lang.String path)
      Normalize the path by suppressing sequences like "path/.." and inner simple dots.
      static boolean codeAwareEqualsIgnoreSpaces​(java.lang.String in1, java.lang.String in2)
      Compares two string being equals ignoring whitespaces, but preserving whitespace between double-quotes The two inputs MUST BE valid DRL/Java syntax (this validation is NOT performed by this method, this method assumes they are).
      static int codeAwareIndexOf​(java.lang.String str, java.lang.String searched)  
      static int codeAwareIndexOf​(java.lang.String str, java.lang.String searched, int fromIndex)  
      static java.lang.String collectionToDelimitedString​(java.util.Collection coll, java.lang.String delim)
      Convenience method to return a Collection as a delimited (e.g.
      static java.lang.String collectionToDelimitedString​(java.util.Collection coll, java.lang.String delim, java.lang.String prefix, java.lang.String suffix)
      Convenience method to return a Collection as a delimited (e.g.
      static java.lang.String deleteAny​(java.lang.String inString, java.lang.String charsToDelete)
      Delete any character in a given String.
      static java.lang.String[] delimitedListToStringArray​(java.lang.String str, java.lang.String delimiter)
      Take a String which is a delimited list and convert it to a String array.
      static java.lang.String[] delimitedListToStringArray​(java.lang.String str, java.lang.String delimiter, java.lang.String charsToDelete)
      Take a String which is a delimited list and convert it to a String array.
      static boolean doesFirstPropHaveListMapAccessor​(java.lang.String expression)  
      static boolean equalsIgnoreSpaces​(java.lang.String s1, java.lang.String s2)  
      static java.lang.String escapeXmlString​(java.lang.String string)  
      static java.lang.String extractFirstIdentifier​(java.lang.String string, int start)  
      static int extractFirstIdentifier​(java.lang.String string, java.lang.StringBuilder builder, int start)  
      static int findEndOfBlockIndex​(java.lang.CharSequence string, int startOfMethodArgsIndex)  
      static int findEndOfMethodArgsIndex​(java.lang.CharSequence string, int startOfMethodArgsIndex)  
      static java.lang.String generateUUID()  
      static java.lang.String getPkgUUID​(java.lang.String gav, java.lang.String packageName)
      Retrieve a consistently reproducible package unique identifier.
      static int indexOfOutOfQuotes​(java.lang.String str, char searched)  
      static int indexOfOutOfQuotes​(java.lang.String str, java.lang.String searched)  
      static int indexOfOutOfQuotes​(java.lang.String str, java.lang.String searched, int fromIndex)  
      static boolean isDereferencingIdentifier​(java.lang.String expr)  
      static boolean isEmpty​(java.lang.CharSequence str)
      Checks if a String is empty ("") or null.
      static boolean isIdentifier​(java.lang.String expr)  
      static java.lang.String lcFirst​(java.lang.String s)  
      static java.lang.String lcFirstForBean​(java.lang.String s)  
      static java.lang.Character lookAheadIgnoringSpaces​(java.lang.String expression, int cursor)  
      static java.lang.String md5Hash​(java.lang.String s)  
      static java.lang.String padding​(int repeat, char padChar)
      Returns padding using the specified delimiter repeated to a given length.
      static java.lang.String readFileAsString​(java.io.Reader reader)  
      static java.lang.String repeat​(java.lang.String str, int repeat)
      Repeat a String repeat times to form a new String.
      static java.lang.String replace​(java.lang.String inString, java.lang.String oldPattern, java.lang.String newPattern)
      Replace all occurences of a substring within a string with another string.
      static java.lang.String replaceOutOfQuotes​(java.lang.String s, java.lang.String oldValue, java.lang.String newValue)  
      static int skipBlanks​(java.lang.String string, int start)  
      static java.util.List<java.lang.String> splitArgumentsList​(java.lang.CharSequence string)  
      static java.util.List<java.lang.String> splitArgumentsList​(java.lang.CharSequence string, boolean trimArgs)  
      static java.util.List<java.lang.String> splitConstraints​(java.lang.CharSequence string, boolean trimArgs)  
      static java.util.List<java.lang.String> splitModifyBlock​(java.lang.CharSequence string)  
      static java.lang.String[] splitPreserveAllTokens​(java.lang.String str, java.lang.String separatorChars)
      Splits the provided text into an array, separators specified, preserving all tokens, including empty tokens created by adjacent separators.
      static java.util.List<java.lang.String> splitStatements​(java.lang.CharSequence string)  
      static java.util.List<java.lang.String> splitStatementsAcrossBlocks​(java.lang.CharSequence string)  
      static double stringSimilarity​(java.lang.String s1, java.lang.String s2, StringUtils.SIMILARITY_STRATS method)  
      static java.lang.String toString​(java.io.BufferedReader reader)  
      static java.lang.String toString​(java.io.InputStream is)  
      static java.lang.String toString​(java.io.Reader reader)  
      static java.lang.String[] toStringArray​(java.util.Collection collection)
      Copy the given Collection into a String array.
      static java.net.URI toURI​(java.lang.String location)  
      static java.lang.String ucFirst​(java.lang.String s)  
      static void unescapeJava​(java.io.Writer out, java.lang.String str)
      Unescapes any Java literals found in the String to a Writer.
      static java.lang.String unescapeJava​(java.lang.String str)
      Unescapes any Java literals found in the String.
      static java.lang.String uuid()  
      • Methods inherited from class java.lang.Object

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

      • EMPTY_STRING_ARRAY

        public static final java.lang.String[] EMPTY_STRING_ARRAY
        An empty immutable String array.
      • EMPTY

        public static final java.lang.String EMPTY
        The empty String "".
        Since:
        2.0
        See Also:
        Constant Field Values
      • INDEX_NOT_FOUND

        public static final int INDEX_NOT_FOUND
        Represents a failed index search.
        Since:
        2.1
        See Also:
        Constant Field Values
    • Constructor Detail

      • StringUtils

        public StringUtils()

        StringUtils instances should NOT be constructed in standard programming. Instead, the class should be used as StringUtils.trim(" foo ");.

        This constructor is public to permit tools that require a JavaBean instance to operate.

    • Method Detail

      • ucFirst

        public static java.lang.String ucFirst​(java.lang.String s)
      • lcFirst

        public static java.lang.String lcFirst​(java.lang.String s)
      • lcFirstForBean

        public static java.lang.String lcFirstForBean​(java.lang.String s)
      • isEmpty

        public static boolean isEmpty​(java.lang.CharSequence str)

        Checks if a String is empty ("") or null.

         StringUtils.isEmpty(null)      = true
         StringUtils.isEmpty("")        = true
         StringUtils.isEmpty(" ")       = false
         StringUtils.isEmpty("bob")     = false
         StringUtils.isEmpty("  bob  ") = false
         

        NOTE: This method changed in Lang version 2.0. It no longer trims the String. That functionality is available in isBlank().

        Parameters:
        str - the String to check, may be null
        Returns:
        true if the String is empty or null
      • repeat

        public static java.lang.String repeat​(java.lang.String str,
                                              int repeat)

        Repeat a String repeat times to form a new String.

         StringUtils.repeat(null, 2) = null
         StringUtils.repeat("", 0)   = ""
         StringUtils.repeat("", 2)   = ""
         StringUtils.repeat("a", 3)  = "aaa"
         StringUtils.repeat("ab", 2) = "abab"
         StringUtils.repeat("a", -2) = ""
         
        Parameters:
        str - the String to repeat, may be null
        repeat - number of times to repeat str, negative treated as zero
        Returns:
        a new String consisting of the original String repeated, null if null String input
      • splitPreserveAllTokens

        public static java.lang.String[] splitPreserveAllTokens​(java.lang.String str,
                                                                java.lang.String separatorChars)

        Splits the provided text into an array, separators specified, preserving all tokens, including empty tokens created by adjacent separators. This is an alternative to using StringTokenizer.

        The separator is not included in the returned String array. Adjacent separators are treated as separators for empty tokens. For more control over the split use the StrTokenizer class.

        A null input String returns null. A null separatorChars splits on whitespace.

         StringUtils.splitPreserveAllTokens(null, *)           = null
         StringUtils.splitPreserveAllTokens("", *)             = []
         StringUtils.splitPreserveAllTokens("abc def", null)   = ["abc", "def"]
         StringUtils.splitPreserveAllTokens("abc def", " ")    = ["abc", "def"]
         StringUtils.splitPreserveAllTokens("abc  def", " ")   = ["abc", "", def"]
         StringUtils.splitPreserveAllTokens("ab:cd:ef", ":")   = ["ab", "cd", "ef"]
         StringUtils.splitPreserveAllTokens("ab:cd:ef:", ":")  = ["ab", "cd", "ef", ""]
         StringUtils.splitPreserveAllTokens("ab:cd:ef::", ":") = ["ab", "cd", "ef", "", ""]
         StringUtils.splitPreserveAllTokens("ab::cd:ef", ":")  = ["ab", "", cd", "ef"]
         StringUtils.splitPreserveAllTokens(":cd:ef", ":")     = ["", cd", "ef"]
         StringUtils.splitPreserveAllTokens("::cd:ef", ":")    = ["", "", cd", "ef"]
         StringUtils.splitPreserveAllTokens(":cd:ef:", ":")    = ["", cd", "ef", ""]
         
        Parameters:
        str - the String to parse, may be null
        separatorChars - the characters used as the delimiters, null splits on whitespace
        Returns:
        an array of parsed Strings, null if null String input
        Since:
        2.1
      • padding

        public static java.lang.String padding​(int repeat,
                                               char padChar)
                                        throws java.lang.IndexOutOfBoundsException

        Returns padding using the specified delimiter repeated to a given length.

         StringUtils.padding(0, 'e')  = ""
         StringUtils.padding(3, 'e')  = "eee"
         StringUtils.padding(-2, 'e') = IndexOutOfBoundsException
         

        Note: this method doesn't not support padding with Unicode Supplementary Characters as they require a pair of chars to be represented. If you are needing to support full I18N of your applications consider using repeat(String, int) instead.

        Parameters:
        repeat - number of times to repeat delim
        padChar - character to repeat
        Returns:
        String with repeated character
        Throws:
        java.lang.IndexOutOfBoundsException - if repeat < 0
        See Also:
        repeat(String, int)
      • readFileAsString

        public static java.lang.String readFileAsString​(java.io.Reader reader)
      • unescapeJava

        public static java.lang.String unescapeJava​(java.lang.String str)

        Unescapes any Java literals found in the String. For example, it will turn a sequence of '\' and 'n' into a newline character, unless the '\' is preceded by another '\'.

        Parameters:
        str - the String to unescape, may be null
        Returns:
        a new unescaped String, null if null string input
      • unescapeJava

        public static void unescapeJava​(java.io.Writer out,
                                        java.lang.String str)
                                 throws java.io.IOException

        Unescapes any Java literals found in the String to a Writer.

        For example, it will turn a sequence of '\' and 'n' into a newline character, unless the '\' is preceded by another '\'.

        A null string input has no effect.

        Parameters:
        out - the Writer used to output unescaped characters
        str - the String to unescape, may be null
        Throws:
        java.lang.IllegalArgumentException - if the Writer is null
        java.io.IOException - if error occurs on underlying Writer
      • cleanPath

        public static java.lang.String cleanPath​(java.lang.String path)
        Normalize the path by suppressing sequences like "path/.." and inner simple dots.

        The result is convenient for path comparison. For other uses, notice that Windows separators ("\") are replaced by simple slashes.

        Parameters:
        path - the original path
        Returns:
        the normalized path Borrowed from Spring, under the ASL2.0 license.
      • collectionToDelimitedString

        public static java.lang.String collectionToDelimitedString​(java.util.Collection coll,
                                                                   java.lang.String delim,
                                                                   java.lang.String prefix,
                                                                   java.lang.String suffix)
        Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.
        Parameters:
        coll - the Collection to display
        delim - the delimiter to use (probably a ",")
        prefix - the String to start each element with
        suffix - the String to end each element with
        Returns:
        the delimited String Borrowed from Spring, under the ASL2.0 license.
      • collectionToDelimitedString

        public static java.lang.String collectionToDelimitedString​(java.util.Collection coll,
                                                                   java.lang.String delim)
        Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.
        Parameters:
        coll - the Collection to display
        delim - the delimiter to use (probably a ",")
        Returns:
        the delimited String Borrowed from Spring, under the ASL2.0 license.
      • replace

        public static java.lang.String replace​(java.lang.String inString,
                                               java.lang.String oldPattern,
                                               java.lang.String newPattern)
        Replace all occurences of a substring within a string with another string.
        Parameters:
        inString - String to examine
        oldPattern - String to replace
        newPattern - String to insert
        Returns:
        a String with the replacements Borrowed from Spring, under the ASL2.0 license.
      • toURI

        public static java.net.URI toURI​(java.lang.String location)
                                  throws java.net.URISyntaxException
        Throws:
        java.net.URISyntaxException
      • escapeXmlString

        public static java.lang.String escapeXmlString​(java.lang.String string)
      • delimitedListToStringArray

        public static java.lang.String[] delimitedListToStringArray​(java.lang.String str,
                                                                    java.lang.String delimiter)
        Take a String which is a delimited list and convert it to a String array.

        A single delimiter can consists of more than one character: It will still be considered as single delimiter string, rather than as bunch of potential delimiter characters - in contrast to tokenizeToStringArray.

        Parameters:
        str - the input String
        delimiter - the delimiter between elements (this is a single delimiter, rather than a bunch individual delimiter characters)
        Returns:
        an array of the tokens in the list Borrowed from Spring, under the ASL2.0 license.
      • delimitedListToStringArray

        public static java.lang.String[] delimitedListToStringArray​(java.lang.String str,
                                                                    java.lang.String delimiter,
                                                                    java.lang.String charsToDelete)
        Take a String which is a delimited list and convert it to a String array.

        A single delimiter can consists of more than one character: It will still be considered as single delimiter string, rather than as bunch of potential delimiter characters - in contrast to tokenizeToStringArray.

        Parameters:
        str - the input String
        delimiter - the delimiter between elements (this is a single delimiter, rather than a bunch individual delimiter characters)
        charsToDelete - a set of characters to delete. Useful for deleting unwanted line breaks: e.g. "\r\n\f" will delete all new lines and line feeds in a String.
        Returns:
        an array of the tokens in the list Borrowed from Spring, under the ASL2.0 license.
      • toStringArray

        public static java.lang.String[] toStringArray​(java.util.Collection collection)
        Copy the given Collection into a String array. The Collection must contain String elements only.
        Parameters:
        collection - the Collection to copy
        Returns:
        the String array (null if the passed-in Collection was null) Borrowed from Spring, under the ASL2.0 license.
      • deleteAny

        public static java.lang.String deleteAny​(java.lang.String inString,
                                                 java.lang.String charsToDelete)
        Delete any character in a given String.
        Parameters:
        inString - the original String
        charsToDelete - a set of characters to delete. E.g. "az\n" will delete 'a's, 'z's and new lines.
        Returns:
        the resulting String Borrowed from Spring, under the ASL2.0 license.
      • toString

        public static java.lang.String toString​(java.io.Reader reader)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • toString

        public static java.lang.String toString​(java.io.InputStream is)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • toString

        public static java.lang.String toString​(java.io.BufferedReader reader)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • getPkgUUID

        public static java.lang.String getPkgUUID​(java.lang.String gav,
                                                  java.lang.String packageName)
        Retrieve a consistently reproducible package unique identifier. It uses both gav and packageName
        Parameters:
        gav -
        packageName -
        Returns:
      • md5Hash

        public static java.lang.String md5Hash​(java.lang.String s)
      • bytesToHex

        public static java.lang.String bytesToHex​(byte[] bytes)
      • generateUUID

        public static java.lang.String generateUUID()
      • extractFirstIdentifier

        public static java.lang.String extractFirstIdentifier​(java.lang.String string,
                                                              int start)
      • extractFirstIdentifier

        public static int extractFirstIdentifier​(java.lang.String string,
                                                 java.lang.StringBuilder builder,
                                                 int start)
      • skipBlanks

        public static int skipBlanks​(java.lang.String string,
                                     int start)
      • splitStatements

        public static java.util.List<java.lang.String> splitStatements​(java.lang.CharSequence string)
      • splitModifyBlock

        public static java.util.List<java.lang.String> splitModifyBlock​(java.lang.CharSequence string)
      • splitStatementsAcrossBlocks

        public static java.util.List<java.lang.String> splitStatementsAcrossBlocks​(java.lang.CharSequence string)
      • splitArgumentsList

        public static java.util.List<java.lang.String> splitArgumentsList​(java.lang.CharSequence string)
      • splitArgumentsList

        public static java.util.List<java.lang.String> splitArgumentsList​(java.lang.CharSequence string,
                                                                          boolean trimArgs)
      • splitConstraints

        public static java.util.List<java.lang.String> splitConstraints​(java.lang.CharSequence string,
                                                                        boolean trimArgs)
      • codeAwareEqualsIgnoreSpaces

        public static boolean codeAwareEqualsIgnoreSpaces​(java.lang.String in1,
                                                          java.lang.String in2)
        Compares two string being equals ignoring whitespaces, but preserving whitespace between double-quotes The two inputs MUST BE valid DRL/Java syntax (this validation is NOT performed by this method, this method assumes they are). Null check: if either of the input is null, this method will return true IFF both are null. Empty check: if either of the input is an empty string, it will be considered as a whitespace during code-aware comparison.
      • findEndOfMethodArgsIndex

        public static int findEndOfMethodArgsIndex​(java.lang.CharSequence string,
                                                   int startOfMethodArgsIndex)
      • findEndOfBlockIndex

        public static int findEndOfBlockIndex​(java.lang.CharSequence string,
                                              int startOfMethodArgsIndex)
      • indexOfOutOfQuotes

        public static int indexOfOutOfQuotes​(java.lang.String str,
                                             java.lang.String searched)
      • indexOfOutOfQuotes

        public static int indexOfOutOfQuotes​(java.lang.String str,
                                             java.lang.String searched,
                                             int fromIndex)
      • codeAwareIndexOf

        public static int codeAwareIndexOf​(java.lang.String str,
                                           java.lang.String searched)
      • codeAwareIndexOf

        public static int codeAwareIndexOf​(java.lang.String str,
                                           java.lang.String searched,
                                           int fromIndex)
      • replaceOutOfQuotes

        public static java.lang.String replaceOutOfQuotes​(java.lang.String s,
                                                          java.lang.String oldValue,
                                                          java.lang.String newValue)
      • indexOfOutOfQuotes

        public static int indexOfOutOfQuotes​(java.lang.String str,
                                             char searched)
      • isIdentifier

        public static boolean isIdentifier​(java.lang.String expr)
      • isDereferencingIdentifier

        public static boolean isDereferencingIdentifier​(java.lang.String expr)
      • equalsIgnoreSpaces

        public static boolean equalsIgnoreSpaces​(java.lang.String s1,
                                                 java.lang.String s2)
      • uuid

        public static java.lang.String uuid()
      • doesFirstPropHaveListMapAccessor

        public static boolean doesFirstPropHaveListMapAccessor​(java.lang.String expression)
      • lookAheadIgnoringSpaces

        public static java.lang.Character lookAheadIgnoringSpaces​(java.lang.String expression,
                                                                  int cursor)