Class StringUtil


  • public final class StringUtil
    extends java.lang.Object
    • 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 letter to title case as per Character.toTitleCase(char).
      static boolean contains​(java.lang.String val, char charToSearchFor)  
      static boolean endsWithIgnoreCase​(java.lang.String str, java.lang.String suffix)  
      static boolean equalsIgnoreCase​(java.lang.String str1, java.lang.String str2)  
      static boolean isBlank​(java.lang.CharSequence cs)
      Checks if a CharSequence is whitespace, empty ("") or null.
      static boolean isEmpty​(java.lang.String val)  
      static boolean isNotBlank​(java.lang.CharSequence cs)
      Checks if a CharSequence is not empty (""), not null and not whitespace only.
      static boolean isNotEmpty​(java.lang.String val)  
      static java.lang.String join​(java.lang.Object[] values, java.lang.String joinToken)  
      static <T> T parse​(java.lang.String val, java.lang.Class<T> type)
      Parse the given
      static java.lang.String replace​(java.lang.String str, java.lang.String from, java.lang.String to)
      Replace all instances of from in str with to.
      static java.lang.String[] split​(java.lang.String str, java.lang.String token, int max)
      Splits the given string on the given token.
      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 trim​(java.lang.String str)
      Null-safe String.trim()
      static java.lang.String trimToNull​(java.lang.String str)  
      static java.lang.String uncapitalize​(java.lang.String str)
      Uncapitalizes a String changing the first letter to title case as per Character.toLowerCase(char).
      • 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.String val)
        Returns:
        true if the given string is null or empty.
      • isNotEmpty

        public static boolean isNotEmpty​(java.lang.String val)
      • isBlank

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

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

         StringUtils.isBlank(null)      = true
         StringUtils.isBlank("")        = true
         StringUtils.isBlank(" ")       = true
         StringUtils.isBlank("bob")     = false
         StringUtils.isBlank("  bob  ") = false
         
        Ported over from Apache commons-lang3
        Parameters:
        cs - the CharSequence to check, may be null
        Returns:
        true if the CharSequence is null, empty or whitespace
      • isNotBlank

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

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

         StringUtils.isNotBlank(null)      = false
         StringUtils.isNotBlank("")        = false
         StringUtils.isNotBlank(" ")       = false
         StringUtils.isNotBlank("bob")     = true
         StringUtils.isNotBlank("  bob  ") = true
         
        Ported over from Apache commons-lang3
        Parameters:
        cs - the CharSequence to check, may be null
        Returns:
        true if the CharSequence is not empty and not null and not whitespace
      • contains

        public static boolean contains​(java.lang.String val,
                                       char charToSearchFor)
        Parameters:
        val - the string to search in
        charToSearchFor - the character to search for
        Returns:
        true if the charToSearchFor is contained in the String val
      • equalsIgnoreCase

        public static boolean equalsIgnoreCase​(java.lang.String str1,
                                               java.lang.String str2)
      • split

        public static java.lang.String[] split​(java.lang.String str,
                                               java.lang.String token,
                                               int max)
        Splits the given string on the given token. Follows the semantics of the Java 1.4 String.split(String, int) method, but does not treat the given token as a regular expression.
      • replace

        public static java.lang.String replace​(java.lang.String str,
                                               java.lang.String from,
                                               java.lang.String to)
        Replace all instances of from in str with to.
        Parameters:
        str - the candidate string to replace
        from - the token to replace
        to - the new token
        Returns:
        the string with all the replacements made
      • trim

        public static java.lang.String trim​(java.lang.String str)
        Null-safe String.trim()
      • trimToNull

        public static java.lang.String trimToNull​(java.lang.String str)
        Returns:
        the trimmed string str or null if the trimmed string would be empty.
      • join

        public static java.lang.String join​(java.lang.Object[] values,
                                            java.lang.String joinToken)
      • parse

        public static <T> T parse​(java.lang.String val,
                                  java.lang.Class<T> type)
        Parse the given
        Parameters:
        val - value to parse
        type - the target type of the the parsed value
        Returns:
        the converted value
      • capitalize

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

        Capitalizes a String changing the first letter to title case as per Character.toTitleCase(char). No other letters are changed.

         StringUtil.capitalize(null)  = null
         StringUtil.capitalize("")    = ""
         StringUtil.capitalize("cat") = "Cat"
         StringUtil.capitalize("cAt") = "CAt"
         
        Ported over from Apache commons-lang3
        Parameters:
        str - the String to capitalize, may be null
        Returns:
        the capitalized String, null if null String input
        See Also:
        uncapitalize(String)
      • uncapitalize

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

        Uncapitalizes a String changing the first letter to title case as per Character.toLowerCase(char). No other letters are changed.

         StringUtil.uncapitalize(null)  = null
         StringUtil.uncapitalize("")    = ""
         StringUtil.uncapitalize("Cat") = "cat"
         StringUtil.uncapitalize("CAT") = "cAT"
         
        Ported over from Apache commons-lang3
        Parameters:
        str - the String to uncapitalize, may be null
        Returns:
        the uncapitalized String, null if null String input
        See Also:
        capitalize(String)
      • endsWithIgnoreCase

        public static boolean endsWithIgnoreCase​(java.lang.String str,
                                                 java.lang.String suffix)
      • 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"
         
        Ported over from Apache commons-lang3
        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