Class StringUtils


  • public final class StringUtils
    extends java.lang.Object

    Operations on String that are null safe.

    • IsEmpty/IsBlank - checks if a String contains text
    • Trim - removes leading and trailing whitespace
    • Equals/Compare - compares two strings null-safe
    • Substring - null-safe substring extraction
    • UpperCase/LowerCase/Capitalize/Uncapitalize - changes the case of a String

    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.

    This class's source was modified from the Apache commons-lang and Apache commons-text libraries: https://github.com/apache/commons-lang/, https://github.com/apache/commons-text/

    #ThreadSafe#

    See Also:
    String, Apache Commons Lang, Apache Commons Text , Elephant bird license
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String capitalize​(java.lang.String str)
      Capitalizes a String changing the first character to title case as per Character.toTitleCase(int).
      static boolean equals​(java.lang.String cs1, java.lang.String cs2)
      Compares two Strings, returning true if they represent equal sequences of characters.
      static java.lang.String escapeJavaString​(java.lang.Object object, java.lang.String indent)  
      static java.lang.String indent​(java.lang.String string, int spaces)
      Indents each line in the provided string by the provided number of spaces.
      static boolean isBlank​(java.lang.CharSequence cs)
      Checks if a CharSequence is empty (""), null or whitespace only.
      static boolean isEmpty​(java.lang.CharSequence cs)
      Checks if a CharSequence is empty ("") or null.
      static boolean isNotBlank​(java.lang.CharSequence cs)
      Checks if a CharSequence is not empty (""), not null and not whitespace only.
      static java.lang.String leftPad​(java.lang.String str, int size)
      Left pad a String with spaces (' ').
      static java.lang.String leftPad​(java.lang.String str, int size, char padChar)
      Left pad a String with a specified character.
      static java.lang.String leftPad​(java.lang.String str, int size, java.lang.String padStr)
      Left pad a String with a specified String.
      static int levenshteinDistance​(java.lang.CharSequence left, java.lang.CharSequence right, int threshold)
      Find the Levenshtein distance between two CharSequences if it's less than or equal to a given threshold.
      static java.lang.String lowerCase​(java.lang.String str)
      Converts a String to lower case as per String.toLowerCase().
      static java.lang.String repeat​(char ch, int repeat)
      Returns padding using the specified delimiter repeated to a given length.
      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 rightPad​(java.lang.String str, int size)
      Right pad a String with spaces (' ').
      static java.lang.String rightPad​(java.lang.String str, int size, char padChar)
      Right pad a String with a specified character.
      static java.lang.String rightPad​(java.lang.String str, int size, java.lang.String padStr)
      Right pad a String with a specified String.
      static boolean startsWithIgnoreCase​(java.lang.String str, java.lang.String prefix)
      Tests if this string starts with the specified prefix ignoring case considerations.
      static java.lang.String strip​(java.lang.String str, java.lang.String stripChars)
      Strips any of a set of characters from the start and end of a String.
      static java.lang.String stripEnd​(java.lang.String str, java.lang.String stripChars)
      Strips any of a set of characters from the end of a String.
      static java.lang.String stripStart​(java.lang.String str, java.lang.String stripChars)
      Strips any of a set of characters from the start of a String.
      static java.lang.String stripToEmpty​(java.lang.String str)
      Strips whitespace from the start and end of a String returning an empty String if null input.
      static java.lang.String substring​(java.lang.String str, int start)
      Gets a substring from the specified String avoiding exceptions.
      static java.lang.String substring​(java.lang.String str, int start, int end)
      Gets a substring from the specified String avoiding exceptions.
      static java.lang.String trim​(java.lang.String str)
      Removes control characters (char <= 32) from both ends of this String, handling null by returning null.
      static java.lang.String trimToEmpty​(java.lang.String str)
      Removes control characters (char <= 32) from both ends of this String returning an empty String ("") if the String is empty ("") after the trim or if it is null.
      static java.lang.String trimToNull​(java.lang.String str)
      Removes control characters (char <= 32) from both ends of this String returning null if the String is empty ("") after the trim or if it is null.
      static java.lang.String uncapitalize​(java.lang.String str)
      Uncapitalizes a String, changing the first character to lower case as per Character.toLowerCase(int).
      static java.lang.String upperCase​(java.lang.String str)
      Converts a String to upper case as per String.toUpperCase().
      static java.lang.String wrap​(java.lang.String str, int wrapLength)
      Wraps a single line of text, identifying words by ' '.
      static java.lang.String wrap​(java.lang.String str, int wrapLength, java.lang.String newLineStr, boolean wrapLongWords)
      Wraps a single line of text, identifying words by ' '.
      static java.lang.String wrap​(java.lang.String str, int wrapLength, java.lang.String newLineStr, boolean wrapLongWords, java.lang.String wrapOn)
      Wraps a single line of text, identifying words by wrapOn.
      • Methods inherited from class java.lang.Object

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

      • isEmpty

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

        Checks if a CharSequence 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 CharSequence. That functionality is available in isBlank().

        Parameters:
        cs - the CharSequence to check, may be null
        Returns:
        true if the CharSequence is empty or null
        Since:
        3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
        See Also:
        Source
      • isBlank

        public static boolean isBlank​(java.lang.CharSequence cs)

        Checks if a CharSequence is empty (""), null or whitespace only.

        Whitespace is defined by Character.isWhitespace(char).

         StringUtils.isBlank(null)      = true
         StringUtils.isBlank("")        = true
         StringUtils.isBlank(" ")       = true
         StringUtils.isBlank("bob")     = false
         StringUtils.isBlank("  bob  ") = false
         
        Parameters:
        cs - the CharSequence to check, may be null
        Returns:
        true if the CharSequence is null, empty or whitespace only
        Since:
        2.0, 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
        See Also:
        Source
      • isNotBlank

        public static boolean isNotBlank​(java.lang.CharSequence cs)

        Checks if a CharSequence is not empty (""), not null and not whitespace only.

        Whitespace is defined by Character.isWhitespace(char).

         StringUtils.isNotBlank(null)      = false
         StringUtils.isNotBlank("")        = false
         StringUtils.isNotBlank(" ")       = false
         StringUtils.isNotBlank("bob")     = true
         StringUtils.isNotBlank("  bob  ") = true
         
        Parameters:
        cs - the CharSequence to check, may be null
        Returns:
        true if the CharSequence is not empty and not null and not whitespace only
        Since:
        2.0, 3.0 Changed signature from isNotBlank(String) to isNotBlank(CharSequence)
        See Also:
        Source
      • trim

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

        Removes control characters (char <= 32) from both ends of this String, handling null by returning null.

        The String is trimmed using String.trim(). Trim removes start and end characters <= 32.

         StringUtils.trim(null)          = null
         StringUtils.trim("")            = ""
         StringUtils.trim("     ")       = ""
         StringUtils.trim("abc")         = "abc"
         StringUtils.trim("    abc    ") = "abc"
         
        Parameters:
        str - the String to be trimmed, may be null
        Returns:
        the trimmed string, null if null String input
        See Also:
        Source
      • trimToNull

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

        Removes control characters (char <= 32) from both ends of this String returning null if the String is empty ("") after the trim or if it is null.

        The String is trimmed using String.trim(). Trim removes start and end characters <= 32.

         StringUtils.trimToNull(null)          = null
         StringUtils.trimToNull("")            = null
         StringUtils.trimToNull("     ")       = null
         StringUtils.trimToNull("abc")         = "abc"
         StringUtils.trimToNull("    abc    ") = "abc"
         
        Parameters:
        str - the String to be trimmed, may be null
        Returns:
        the trimmed String, null if only chars <= 32, empty or null String input
        Since:
        2.0
        See Also:
        Source
      • trimToEmpty

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

        Removes control characters (char <= 32) from both ends of this String returning an empty String ("") if the String is empty ("") after the trim or if it is null.

        The String is trimmed using String.trim(). Trim removes start and end characters <= 32.

         StringUtils.trimToEmpty(null)          = ""
         StringUtils.trimToEmpty("")            = ""
         StringUtils.trimToEmpty("     ")       = ""
         StringUtils.trimToEmpty("abc")         = "abc"
         StringUtils.trimToEmpty("    abc    ") = "abc"
         
        Parameters:
        str - the String to be trimmed, may be null
        Returns:
        the trimmed String, or an empty String if null input
        Since:
        2.0
        See Also:
        Source
      • equals

        public static boolean equals​(java.lang.String cs1,
                                     java.lang.String cs2)

        Compares two Strings, returning true if they represent equal sequences of characters.

        nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case sensitive.

         StringUtils.equals(null, null)   = true
         StringUtils.equals(null, "abc")  = false
         StringUtils.equals("abc", null)  = false
         StringUtils.equals("abc", "abc") = true
         StringUtils.equals("abc", "ABC") = false
         
        Parameters:
        cs1 - the first String, may be null
        cs2 - the second String, may be null
        Returns:
        true if the Strings are equal (case-sensitive), or both null
        See Also:
        Object.equals(Object), Source
      • substring

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

        Gets a substring from the specified String avoiding exceptions.

        A negative start position can be used to start n characters from the end of the String.

        A null String will return null. An empty ("") String will return "".

         StringUtils.substring(null, *)   = null
         StringUtils.substring("", *)     = ""
         StringUtils.substring("abc", 0)  = "abc"
         StringUtils.substring("abc", 2)  = "c"
         StringUtils.substring("abc", 4)  = ""
         StringUtils.substring("abc", -2) = "bc"
         StringUtils.substring("abc", -4) = "abc"
         
        Parameters:
        str - the String to get the substring from, may be null
        start - the position to start from, negative means count back from the end of the String by this many characters
        Returns:
        substring from start position, null if null String input
        See Also:
        Source
      • substring

        public static java.lang.String substring​(java.lang.String str,
                                                 int start,
                                                 int end)

        Gets a substring from the specified String avoiding exceptions.

        A negative start position can be used to start/end n characters from the end of the String.

        The returned substring starts with the character in the start position and ends before the end position. All position counting is zero-based -- i.e., to start at the beginning of the string use start = 0. Negative start and end positions can be used to specify offsets relative to the end of the String.

        If start is not strictly to the left of end, "" is returned.

         StringUtils.substring(null, *, *)    = null
         StringUtils.substring("", * ,  *)    = "";
         StringUtils.substring("abc", 0, 2)   = "ab"
         StringUtils.substring("abc", 2, 0)   = ""
         StringUtils.substring("abc", 2, 4)   = "c"
         StringUtils.substring("abc", 4, 6)   = ""
         StringUtils.substring("abc", 2, 2)   = ""
         StringUtils.substring("abc", -2, -1) = "b"
         StringUtils.substring("abc", -4, 2)  = "ab"
         
        Parameters:
        str - the String to get the substring from, may be null
        start - the position to start from, negative means count back from the end of the String by this many characters
        end - the position to end at (exclusive), negative means count back from the end of the String by this many scharacters
        Returns:
        substring from start position to end position, null if null String input
        See Also:
        Source
      • upperCase

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

        Converts a String to upper case as per String.toUpperCase().

        A null input String returns null.

         StringUtils.upperCase(null)  = null
         StringUtils.upperCase("")    = ""
         StringUtils.upperCase("aBc") = "ABC"
         

        This uses "ENGLISH" as the locale.

        Parameters:
        str - the String to upper case, may be null
        Returns:
        the upper cased String, null if null String input
        See Also:
        Source
      • lowerCase

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

        Converts a String to lower case as per String.toLowerCase().

        A null input String returns null.

         StringUtils.lowerCase(null)  = null
         StringUtils.lowerCase("")    = ""
         StringUtils.lowerCase("aBc") = "abc"
         

        This uses "ENGLISH" as the locale.

        Parameters:
        str - the String to lower case, may be null
        Returns:
        the lower cased String, null if null String input
        See Also:
        Source
      • capitalize

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

        Capitalizes a String changing the first character to title case as per Character.toTitleCase(int). No other characters are changed.

         StringUtils.capitalize(null)  = null
         StringUtils.capitalize("")    = ""
         StringUtils.capitalize("cat") = "Cat"
         StringUtils.capitalize("cAt") = "CAt"
         StringUtils.capitalize("'cat'") = "'cat'"
         
        Parameters:
        str - the String to capitalize, may be null
        Returns:
        the capitalized String, null if null String input
        Since:
        2.0
        See Also:
        uncapitalize(String), Source
      • uncapitalize

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

        Uncapitalizes a String, changing the first character to lower case as per Character.toLowerCase(int). No other characters are changed.

         StringUtils.uncapitalize(null)  = null
         StringUtils.uncapitalize("")    = ""
         StringUtils.uncapitalize("cat") = "cat"
         StringUtils.uncapitalize("Cat") = "cat"
         StringUtils.uncapitalize("CAT") = "cAT"
         
        Parameters:
        str - the String to uncapitalize, may be null
        Returns:
        the uncapitalized String, null if null String input
        Since:
        2.0
        See Also:
        capitalize(String), Source
      • startsWithIgnoreCase

        public static boolean startsWithIgnoreCase​(java.lang.String str,
                                                   java.lang.String prefix)
        Tests if this string starts with the specified prefix ignoring case considerations.
        Parameters:
        str - the string to be tested
        prefix - the prefix
        Returns:
        true if the string starts with the prefix ignoring case
      • 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
        See Also:
        Source
      • repeat

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

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

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

        Note: this method does 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:
        ch - character to repeat
        repeat - number of times to repeat char, negative treated as zero
        Returns:
        String with repeated character
        See Also:
        repeat(String, int), Source
      • rightPad

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

        Right pad a String with spaces (' ').

        The String is padded to the size of size.

         StringUtils.rightPad(null, *)   = null
         StringUtils.rightPad("", 3)     = "   "
         StringUtils.rightPad("bat", 3)  = "bat"
         StringUtils.rightPad("bat", 5)  = "bat  "
         StringUtils.rightPad("bat", 1)  = "bat"
         StringUtils.rightPad("bat", -1) = "bat"
         
        Parameters:
        str - the String to pad out, may be null
        size - the size to pad to
        Returns:
        right padded String or original String if no padding is necessary, null if null String input
        See Also:
        Source
      • rightPad

        public static java.lang.String rightPad​(java.lang.String str,
                                                int size,
                                                char padChar)

        Right pad a String with a specified character.

        The String is padded to the size of size.

         StringUtils.rightPad(null, *, *)     = null
         StringUtils.rightPad("", 3, 'z')     = "zzz"
         StringUtils.rightPad("bat", 3, 'z')  = "bat"
         StringUtils.rightPad("bat", 5, 'z')  = "batzz"
         StringUtils.rightPad("bat", 1, 'z')  = "bat"
         StringUtils.rightPad("bat", -1, 'z') = "bat"
         
        Parameters:
        str - the String to pad out, may be null
        size - the size to pad to
        padChar - the character to pad with
        Returns:
        right padded String or original String if no padding is necessary, null if null String input
        Since:
        2.0
        See Also:
        Source
      • rightPad

        public static java.lang.String rightPad​(java.lang.String str,
                                                int size,
                                                java.lang.String padStr)

        Right pad a String with a specified String.

        The String is padded to the size of size.

         StringUtils.rightPad(null, *, *)      = null
         StringUtils.rightPad("", 3, "z")      = "zzz"
         StringUtils.rightPad("bat", 3, "yz")  = "bat"
         StringUtils.rightPad("bat", 5, "yz")  = "batyz"
         StringUtils.rightPad("bat", 8, "yz")  = "batyzyzy"
         StringUtils.rightPad("bat", 1, "yz")  = "bat"
         StringUtils.rightPad("bat", -1, "yz") = "bat"
         StringUtils.rightPad("bat", 5, null)  = "bat  "
         StringUtils.rightPad("bat", 5, "")    = "bat  "
         
        Parameters:
        str - the String to pad out, may be null
        size - the size to pad to
        padStr - the String to pad with, null or empty treated as single space
        Returns:
        right padded String or original String if no padding is necessary, null if null String input
        See Also:
        Source
      • leftPad

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

        Left pad a String with spaces (' ').

        The String is padded to the size of size.

         StringUtils.leftPad(null, *)   = null
         StringUtils.leftPad("", 3)     = "   "
         StringUtils.leftPad("bat", 3)  = "bat"
         StringUtils.leftPad("bat", 5)  = "  bat"
         StringUtils.leftPad("bat", 1)  = "bat"
         StringUtils.leftPad("bat", -1) = "bat"
         
        Parameters:
        str - the String to pad out, may be null
        size - the size to pad to
        Returns:
        left padded String or original String if no padding is necessary, null if null String input
        See Also:
        Source
      • leftPad

        public static java.lang.String leftPad​(java.lang.String str,
                                               int size,
                                               char padChar)

        Left pad a String with a specified character.

        Pad to a size of size.

         StringUtils.leftPad(null, *, *)     = null
         StringUtils.leftPad("", 3, 'z')     = "zzz"
         StringUtils.leftPad("bat", 3, 'z')  = "bat"
         StringUtils.leftPad("bat", 5, 'z')  = "zzbat"
         StringUtils.leftPad("bat", 1, 'z')  = "bat"
         StringUtils.leftPad("bat", -1, 'z') = "bat"
         
        Parameters:
        str - the String to pad out, may be null
        size - the size to pad to
        padChar - the character to pad with
        Returns:
        left padded String or original String if no padding is necessary, null if null String input
        Since:
        2.0
        See Also:
        Source
      • leftPad

        public static java.lang.String leftPad​(java.lang.String str,
                                               int size,
                                               java.lang.String padStr)

        Left pad a String with a specified String.

        Pad to a size of size.

         StringUtils.leftPad(null, *, *)      = null
         StringUtils.leftPad("", 3, "z")      = "zzz"
         StringUtils.leftPad("bat", 3, "yz")  = "bat"
         StringUtils.leftPad("bat", 5, "yz")  = "yzbat"
         StringUtils.leftPad("bat", 8, "yz")  = "yzyzybat"
         StringUtils.leftPad("bat", 1, "yz")  = "bat"
         StringUtils.leftPad("bat", -1, "yz") = "bat"
         StringUtils.leftPad("bat", 5, null)  = "  bat"
         StringUtils.leftPad("bat", 5, "")    = "  bat"
         
        Parameters:
        str - the String to pad out, may be null
        size - the size to pad to
        padStr - the String to pad with, null or empty treated as single space
        Returns:
        left padded String or original String if no padding is necessary, null if null String input
        See Also:
        Source
      • strip

        public static java.lang.String strip​(java.lang.String str,
                                             java.lang.String stripChars)

        Strips any of a set of characters from the start and end of a String. This is similar to String.trim() but allows the characters to be stripped to be controlled.

        A null input String returns null. An empty string ("") input returns the empty string.

        If the stripChars String is null, whitespace is stripped as defined by Character.isWhitespace(char).

         StringUtils.strip(null, *)          = null
         StringUtils.strip("", *)            = ""
         StringUtils.strip("abc", null)      = "abc"
         StringUtils.strip("  abc", null)    = "abc"
         StringUtils.strip("abc  ", null)    = "abc"
         StringUtils.strip(" abc ", null)    = "abc"
         StringUtils.strip("  abcyx", "xyz") = "  abc"
         
        Parameters:
        str - the String to remove characters from, may be null
        stripChars - the characters to remove, null treated as whitespace
        Returns:
        the stripped String, null if null String input
        See Also:
        Source
      • stripEnd

        public static java.lang.String stripEnd​(java.lang.String str,
                                                java.lang.String stripChars)

        Strips any of a set of characters from the end of a String.

        A null input String returns null. An empty string ("") input returns the empty string.

        If the stripChars String is null, whitespace is stripped as defined by Character.isWhitespace(char).

         StringUtils.stripEnd(null, *)          = null
         StringUtils.stripEnd("", *)            = ""
         StringUtils.stripEnd("abc", "")        = "abc"
         StringUtils.stripEnd("abc", null)      = "abc"
         StringUtils.stripEnd("  abc", null)    = "  abc"
         StringUtils.stripEnd("abc  ", null)    = "abc"
         StringUtils.stripEnd(" abc ", null)    = " abc"
         StringUtils.stripEnd("  abcyx", "xyz") = "  abc"
         StringUtils.stripEnd("120.00", ".0")   = "12"
         
        Parameters:
        str - the String to remove characters from, may be null
        stripChars - the set of characters to remove, null treated as whitespace
        Returns:
        the stripped String, null if null String input
        See Also:
        Source
      • stripStart

        public static java.lang.String stripStart​(java.lang.String str,
                                                  java.lang.String stripChars)

        Strips any of a set of characters from the start of a String.

        A null input String returns null. An empty string ("") input returns the empty string.

        If the stripChars String is null, whitespace is stripped as defined by Character.isWhitespace(char).

         StringUtils.stripStart(null, *)          = null
         StringUtils.stripStart("", *)            = ""
         StringUtils.stripStart("abc", "")        = "abc"
         StringUtils.stripStart("abc", null)      = "abc"
         StringUtils.stripStart("  abc", null)    = "abc"
         StringUtils.stripStart("abc  ", null)    = "abc  "
         StringUtils.stripStart(" abc ", null)    = "abc "
         StringUtils.stripStart("yxabc  ", "xyz") = "abc  "
         
        Parameters:
        str - the String to remove characters from, may be null
        stripChars - the characters to remove, null treated as whitespace
        Returns:
        the stripped String, null if null String input
        See Also:
        Source
      • stripToEmpty

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

        Strips whitespace from the start and end of a String returning an empty String if null input.

        This is similar to trimToEmpty(String) but removes whitespace. Whitespace is defined by Character.isWhitespace(char).

         StringUtils.stripToEmpty(null)     = ""
         StringUtils.stripToEmpty("")       = ""
         StringUtils.stripToEmpty("   ")    = ""
         StringUtils.stripToEmpty("abc")    = "abc"
         StringUtils.stripToEmpty("  abc")  = "abc"
         StringUtils.stripToEmpty("abc  ")  = "abc"
         StringUtils.stripToEmpty(" abc ")  = "abc"
         StringUtils.stripToEmpty(" ab c ") = "ab c"
         
        Parameters:
        str - the String to be stripped, may be null
        Returns:
        the trimmed String, or an empty String if null input
        Since:
        2.0
        See Also:
        Source
      • wrap

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

        Wraps a single line of text, identifying words by ' '.

        New lines will be separated by the system property line separator. Very long words, such as URLs will not be wrapped.

        Leading spaces on a new line are stripped. Trailing spaces are not stripped.

        Examples
        input wrapLength result
        null * null
        "" * ""
        "Here is one line of text that is going to be wrapped after 20 columns." 20 "Here is one line of\ntext that is going\nto be wrapped after\n20 columns."
        "Click here to jump to the commons website - http://commons.apache.org" 20 "Click here to jump\nto the commons\nwebsite -\nhttp://commons.apache.org"
        "Click here, http://commons.apache.org, to jump to the commons website" 20 "Click here,\nhttp://commons.apache.org,\nto jump to the\ncommons website"
        (assuming that '\n' is the systems line separator)
        Parameters:
        str - the String to be word wrapped, may be null
        wrapLength - the column to wrap the words at, less than 1 is treated as 1
        Returns:
        a line with newlines inserted, null if null input
        See Also:
        Source
      • wrap

        public static java.lang.String wrap​(java.lang.String str,
                                            int wrapLength,
                                            java.lang.String newLineStr,
                                            boolean wrapLongWords)

        Wraps a single line of text, identifying words by ' '.

        Leading spaces on a new line are stripped. Trailing spaces are not stripped.

        Examples
        input wrapLength newLineString wrapLongWords result
        null * * true/false null
        "" * * true/false ""
        "Here is one line of text that is going to be wrapped after 20 columns." 20 "\n" true/false "Here is one line of\ntext that is going\nto be wrapped after\n20 columns."
        "Here is one line of text that is going to be wrapped after 20 columns." 20 "<br />" true/false "Here is one line of<br />text that is going< br />to be wrapped after<br />20 columns."
        "Here is one line of text that is going to be wrapped after 20 columns." 20 null true/false "Here is one line of" + systemNewLine + "text that is going" + systemNewLine + "to be wrapped after" + systemNewLine + "20 columns."
        "Click here to jump to the commons website - http://commons.apache.org" 20 "\n" false "Click here to jump\nto the commons\nwebsite -\nhttp://commons.apache.org"
        "Click here to jump to the commons website - http://commons.apache.org" 20 "\n" true "Click here to jump\nto the commons\nwebsite -\nhttp://commons.apach\ne.org"
        Parameters:
        str - the String to be word wrapped, may be null
        wrapLength - the column to wrap the words at, less than 1 is treated as 1
        newLineStr - the string to insert for a new line, null uses the system property line separator
        wrapLongWords - true if long words (such as URLs) should be wrapped
        Returns:
        a line with newlines inserted, null if null input
        See Also:
        Source
      • wrap

        public static java.lang.String wrap​(java.lang.String str,
                                            int wrapLength,
                                            java.lang.String newLineStr,
                                            boolean wrapLongWords,
                                            java.lang.String wrapOn)

        Wraps a single line of text, identifying words by wrapOn.

        Leading spaces on a new line are stripped. Trailing spaces are not stripped.

        Examples
        input wrapLength newLineString wrapLongWords wrapOn result
        null * * true/false * null
        "" * * true/false * ""
        "Here is one line of text that is going to be wrapped after 20 columns." 20 "\n" true/false " " "Here is one line of\ntext that is going\nto be wrapped after\n20 columns."
        "Here is one line of text that is going to be wrapped after 20 columns." 20 "<br />" true/false " " "Here is one line of<br />text that is going<br /> to be wrapped after<br />20 columns."
        "Here is one line of text that is going to be wrapped after 20 columns." 20 null true/false " " "Here is one line of" + systemNewLine + "text that is going" + systemNewLine + "to be wrapped after" + systemNewLine + "20 columns."
        "Click here to jump to the commons website - http://commons.apache.org" 20 "\n" false " " "Click here to jump\nto the commons\nwebsite -\nhttp://commons.apache.org"
        "Click here to jump to the commons website - http://commons.apache.org" 20 "\n" true " " "Click here to jump\nto the commons\nwebsite -\nhttp://commons.apach\ne.org"
        "flammable/inflammable" 20 "\n" true "/" "flammable\ninflammable"
        Parameters:
        str - the String to be word wrapped, may be null
        wrapLength - the column to wrap the words at, less than 1 is treated as 1
        newLineStr - the string to insert for a new line, null uses the system property line separator
        wrapLongWords - true if long words (such as URLs) should be wrapped
        wrapOn - regex expression to be used as a breakable characters, if blank string is provided a space character will be used
        Returns:
        a line with newlines inserted, null if null input
        See Also:
        Source
      • escapeJavaString

        public static java.lang.String escapeJavaString​(java.lang.Object object,
                                                        java.lang.String indent)
      • levenshteinDistance

        public static int levenshteinDistance​(java.lang.CharSequence left,
                                              java.lang.CharSequence right,
                                              int threshold)
        Find the Levenshtein distance between two CharSequences if it's less than or equal to a given threshold.

        This code is a copy of the limitedCompare method from Apache commons-text LevenshteinDistance.java.

        This implementation follows from Algorithms on Strings, Trees and Sequences by Dan Gusfield and Chas Emerick's implementation of the Levenshtein distance algorithm from http://www.merriampark.com/ld.htm

         limitedCompare(null, *, *)             = IllegalArgumentException
         limitedCompare(*, null, *)             = IllegalArgumentException
         limitedCompare(*, *, -1)               = IllegalArgumentException
         limitedCompare("","", 0)               = 0
         limitedCompare("aaapppp", "", 8)       = 7
         limitedCompare("aaapppp", "", 7)       = 7
         limitedCompare("aaapppp", "", 6))      = -1
         limitedCompare("elephant", "hippo", 7) = 7
         limitedCompare("elephant", "hippo", 6) = -1
         limitedCompare("hippo", "elephant", 7) = 7
         limitedCompare("hippo", "elephant", 6) = -1
         
        Parameters:
        left - the first CharSequence, must not be null
        right - the second CharSequence, must not be null
        threshold - the target threshold, must not be negative
        Returns:
        result distance, or -1
        See Also:
        LevenshteinDistance.java
      • indent

        public static java.lang.String indent​(java.lang.String string,
                                              int spaces)
        Indents each line in the provided string by the provided number of spaces.
        Parameters:
        string - the string to indent.
        spaces - the number of spaces.
        Returns:
        the indented string.