Class StringUtils


  • public class StringUtils
    extends java.lang.Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String[] EMPTY_STRING_ARRAY
      An empty immutable String array.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void escapeNumericChar​(java.io.Writer out, java.lang.String str)
      write the escaped version of a given string
      static java.lang.String escapeNumericChar​(java.lang.String str)
      write the escaped version of a given string
      static boolean isEmpty​(java.lang.String str)
      Checks if a String is empty ("") or null.
      static java.lang.String[] split​(java.lang.String str, char separatorChar)
      Splits the provided text into an array, separator specified.
      static boolean startsWithIgnoreWhitespaces​(java.lang.String prefix, java.lang.String string)
      Tests if this string starts with the specified prefix (Ignoring whitespaces)
      static java.lang.String strip​(java.lang.String str)
      Strips whitespace from the start and end of a String.
      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 void unescapeNumericChar​(java.io.Writer out, java.lang.String str)
      Unescapes numeric character references found in the String to a Writer.
      static java.lang.String unescapeNumericChar​(java.lang.String str)
      Unescapes numeric character referencs found in the String.
      • 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.
    • Method Detail

      • startsWithIgnoreWhitespaces

        public static boolean startsWithIgnoreWhitespaces​(java.lang.String prefix,
                                                          java.lang.String string)
        Tests if this string starts with the specified prefix (Ignoring whitespaces)
        Parameters:
        prefix -
        string -
        Returns:
        boolean
      • split

        public static java.lang.String[] split​(java.lang.String str,
                                               char separatorChar)

        Splits the provided text into an array, separator specified. This is an alternative to using StringTokenizer.

        The separator is not included in the returned String array. Adjacent separators are treated as one separator.

        A null input String returns null.

         StringUtils.split(null, *)         = null
         StringUtils.split("", *)           = []
         StringUtils.split("a.b.c", '.')    = ["a", "b", "c"]
         StringUtils.split("a..b.c", '.')   = ["a", "b", "c"]
         StringUtils.split("a:b:c", '.')    = ["a:b:c"]
         StringUtils.split("a\tb\nc", null) = ["a", "b", "c"]
         StringUtils.split("a b c", ' ')    = ["a", "b", "c"]
         
        Parameters:
        str - the String to parse, may be null
        separatorChar - the character used as the delimiter, null splits on whitespace
        Returns:
        an array of parsed Strings, null if null String input
      • isEmpty

        public static boolean isEmpty​(java.lang.String 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
      • strip

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

        Strips whitespace from the start and end of a String.

        This removes whitespace. Whitespace is defined by Character.isWhitespace(char).

        A null input String returns null.

         StringUtils.strip(null)     = null
         StringUtils.strip("")       = ""
         StringUtils.strip("   ")    = ""
         StringUtils.strip("abc")    = "abc"
         StringUtils.strip("  abc")  = "abc"
         StringUtils.strip("abc  ")  = "abc"
         StringUtils.strip(" abc ")  = "abc"
         StringUtils.strip(" ab c ") = "ab c"
         
        Parameters:
        str - the String to remove whitespace from, may be null
        Returns:
        the stripped String, null if null String input
      • 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). Alternatively use strip(String).

         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
      • 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
      • 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"
         
        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
      • escapeNumericChar

        public static java.lang.String escapeNumericChar​(java.lang.String str)
        write the escaped version of a given string
        Parameters:
        str - string to be encoded
        Returns:
        a new escaped String, null if null string input
      • escapeNumericChar

        public static void escapeNumericChar​(java.io.Writer out,
                                             java.lang.String str)
                                      throws java.io.IOException
        write the escaped version of a given string
        Parameters:
        out - writer to write this string to
        str - string to be encoded
        Throws:
        java.io.IOException
      • unescapeNumericChar

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

        Unescapes numeric character referencs found in the String.

        For example, it will return a unicode string which means the specified numeric character references looks like "ようこそ".

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

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

        Unescapes numeric character references found in the String to a Writer.

        For example, it will return a unicode string which means the specified numeric character references looks like "ようこそ".

        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