Class StringUtils


  • public class StringUtils
    extends java.lang.Object
    Static utility methods pertaining to String or CharSequence instances.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String EMPTY
      The empty String
    • Constructor Summary

      Constructors 
      Constructor Description
      StringUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean containsWhitespace​(java.lang.CharSequence str)
      Check whether the given CharSequence contains any whitespace characters.
      static boolean containsWhitespace​(java.lang.String str)
      Check whether the given String contains any whitespace characters.
      static java.lang.String convertToHumanFriendlyByteSize​(long size)
      Convert byte size into human friendly format.
      static long convertToMachineFriendlyByteSize​(java.lang.String size)
      Convert byte size into machine friendly format.
      static java.lang.String emptyToNull​(java.lang.String str)
      Returns the given string if it is nonempty; null otherwise.
      static boolean endsWith​(java.lang.String str, char suffix)
      Test if the given String ends with the specified prefix character.
      static boolean endsWithIgnoreCase​(java.lang.String str, java.lang.String suffix)
      Test if the given String ends with the specified suffix, ignoring upper/lower case.
      static boolean hasLength​(java.lang.CharSequence chars)
      Check that the given CharSequence is neither null nor of length 0.
      static boolean hasLength​(java.lang.String str)
      Check that the given String is neither null nor of length 0.
      static boolean hasText​(java.lang.CharSequence str)
      Check whether the given CharSequence contains actual text.
      static boolean hasText​(java.lang.String str)
      Check whether the given String contains actual text.
      static boolean isEmpty​(java.lang.String str)
      Returns true if the given string is null or is the empty string.
      static java.lang.String joinCommaDelimitedList​(java.lang.String[] arr)
      Convert a String array into a comma delimited String (i.e., CSV).
      static java.lang.String joinCommaDelimitedList​(java.util.Collection<?> list)
      Convert a Collection into a comma delimited String (i.e., CSV).
      static java.lang.String nullToEmpty​(java.lang.String str)
      Returns the given string if it is non-null; the empty string otherwise.
      static java.util.Locale parseLocaleString​(java.lang.String localeString)
      Parse the given localeString value into a Locale.
      static java.util.TimeZone parseTimeZoneString​(java.lang.String timeZoneString)
      Parse the given timeZoneString value into a TimeZone.
      static java.lang.String repeat​(char ch, int repeat)
      Returns padding using the specified delimiter repeated to a given length.
      static java.lang.String replace​(java.lang.String str, java.lang.String[] search, java.lang.String[] replace)
      Replace all occurrences of a substring within a string with another string.
      static java.lang.String replace​(java.lang.String str, java.lang.String search, java.lang.String replace)
      Replace all occurrences of a substring within a string with another string.
      static int search​(java.lang.CharSequence chars, char c)
      Returns the number of times the specified character was found in the target string, or 0 if there is no specified character.
      static int search​(java.lang.String str, java.lang.String keyw)
      Returns the number of times the specified string was found in the target string, or 0 if there is no specified string.
      static int searchIgnoreCase​(java.lang.CharSequence chars, char c)
      Returns the number of times the specified character was found in the target string, or 0 if there is no specified character.
      static int searchIgnoreCase​(java.lang.String str, java.lang.String keyw)
      Returns the number of times the specified string was found in the target string, or 0 if there is no specified string.
      static java.lang.String[] split​(java.lang.String str, char delim)
      Returns an array of strings separated by the delimiter string.
      static java.lang.String[] split​(java.lang.String str, char delim, int size)
      Returns an array of strings separated by the delimiter string.
      static java.lang.String[] split​(java.lang.String str, java.lang.String delim)
      Returns an array of strings separated by the delimiter string.
      static java.lang.String[] split​(java.lang.String str, java.lang.String delim, int size)
      Returns an array of strings separated by the delimiter string.
      static java.lang.String[] splitCommaDelimitedString​(java.lang.String str)
      Convert a comma delimited list (e.g., a row from a CSV file) into an array of strings.
      static boolean startsWith​(java.lang.String str, char prefix)
      Test if the given String starts with the specified prefix character.
      static boolean startsWithIgnoreCase​(java.lang.String str, java.lang.String prefix)
      Test if the given String starts with the specified prefix, ignoring upper/lower case.
      static java.lang.String toDelimitedString​(java.lang.Object[] arr, java.lang.String delim)
      Convert a String array into a delimited String (e.g.
      static java.lang.String toDelimitedString​(java.util.Collection<?> list, java.lang.String delim)
      Convert a Collection into a delimited String (e.g.
      static java.lang.String[] tokenize​(java.lang.String str, java.lang.String delimiters)
      Tokenize the given String into a String array via a StringTokenizer.
      static java.lang.String[] tokenize​(java.lang.String str, java.lang.String delimiters, boolean trim)
      Tokenize the given String into a String array via a StringTokenizer.
      static java.lang.String toLanguageTag​(java.util.Locale locale)
      Determine the RFC 3066 compliant language tag, as used for the HTTP "Accept-Language" header.
      static java.lang.String toLineDelimitedString​(java.lang.Object[] arr)
      Convert a String array into a delimited String by a system-dependent line separator.
      static java.lang.String toLineDelimitedString​(java.util.Collection<?> list)
      Convert a Collection into a delimited String by a system-dependent line separator.
      static java.lang.String trimAllWhitespace​(java.lang.String str)
      Trim all whitespace from the given String: leading, trailing, and in between characters.
      static java.lang.String trimLeadingCharacter​(java.lang.String str, char leadingCharacter)
      Trim all occurrences of the supplied leading character from the given String.
      static java.lang.String trimLeadingWhitespace​(java.lang.String str)
      Trim leading whitespace from the given String.
      static java.lang.String trimTrailingCharacter​(java.lang.String str, char trailingCharacter)
      Trim all occurrences of the supplied trailing character from the given String.
      static java.lang.String trimTrailingWhitespace​(java.lang.String str)
      Trim trailing whitespace from the given String.
      static java.lang.String trimWhitespace​(java.lang.String str)
      Trim leading and trailing whitespace from the given String.
      • Methods inherited from class java.lang.Object

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

      • StringUtils

        public StringUtils()
    • Method Detail

      • hasLength

        public static boolean hasLength​(java.lang.CharSequence chars)
        Check that the given CharSequence is neither null nor of length 0.

        Note: this method returns true for a CharSequence that purely consists of whitespace.

         StringUtils.hasLength(null) = false
         StringUtils.hasLength("") = false
         StringUtils.hasLength(" ") = true
         StringUtils.hasLength("Hello") = true
         
        Parameters:
        chars - the CharSequence to check (may be null)
        Returns:
        true if the CharSequence is not null and has length
        See Also:
        hasText(String)
      • hasLength

        public static boolean hasLength​(java.lang.String str)
        Check that the given String is neither null nor of length 0.

        Note: this method returns true for a String that purely consists of whitespace.

        Parameters:
        str - the String to check (may be null)
        Returns:
        true if the String is not null and has length
        See Also:
        hasLength(CharSequence), hasText(String)
      • hasText

        public static boolean hasText​(java.lang.CharSequence str)
        Check whether the given CharSequence contains actual text.

        More specifically, this method returns true if the CharSequence is not null, its length is greater than 0, and it contains at least one non-whitespace character.

         StringUtils.hasText(null) = false
         StringUtils.hasText("") = false
         StringUtils.hasText(" ") = false
         StringUtils.hasText("12345") = true
         StringUtils.hasText(" 12345 ") = true
         
        Parameters:
        str - the CharSequence to check (may be null)
        Returns:
        true if the CharSequence is not null, its length is greater than 0, and it does not contain whitespace only
        See Also:
        Character.isWhitespace(char)
      • hasText

        public static boolean hasText​(java.lang.String str)
        Check whether the given String contains actual text.

        More specifically, this method returns true if the String is not null, its length is greater than 0, and it contains at least one non-whitespace character.

        Parameters:
        str - the String to check (may be null)
        Returns:
        true if the String is not null, its length is greater than 0, and it does not contain whitespace only
        See Also:
        hasText(CharSequence)
      • containsWhitespace

        public static boolean containsWhitespace​(java.lang.CharSequence str)
        Check whether the given CharSequence contains any whitespace characters.
        Parameters:
        str - the CharSequence to check (may be null)
        Returns:
        true if the CharSequence is not empty and contains at least 1 whitespace character
        See Also:
        Character.isWhitespace(char)
      • containsWhitespace

        public static boolean containsWhitespace​(java.lang.String str)
        Check whether the given String contains any whitespace characters.
        Parameters:
        str - the String to check (may be null)
        Returns:
        true if the String is not empty and contains at least 1 whitespace character
        See Also:
        containsWhitespace(CharSequence)
      • trimWhitespace

        public static java.lang.String trimWhitespace​(java.lang.String str)
        Trim leading and trailing whitespace from the given String.
        Parameters:
        str - the String to check
        Returns:
        the trimmed String
        See Also:
        Character.isWhitespace(char)
      • trimAllWhitespace

        public static java.lang.String trimAllWhitespace​(java.lang.String str)
        Trim all whitespace from the given String: leading, trailing, and in between characters.
        Parameters:
        str - the String to check
        Returns:
        the trimmed String
        See Also:
        Character.isWhitespace(char)
      • trimLeadingWhitespace

        public static java.lang.String trimLeadingWhitespace​(java.lang.String str)
        Trim leading whitespace from the given String.
        Parameters:
        str - the String to check
        Returns:
        the trimmed String
        See Also:
        Character.isWhitespace(char)
      • trimTrailingWhitespace

        public static java.lang.String trimTrailingWhitespace​(java.lang.String str)
        Trim trailing whitespace from the given String.
        Parameters:
        str - the String to check
        Returns:
        the trimmed String
        See Also:
        Character.isWhitespace(char)
      • trimLeadingCharacter

        public static java.lang.String trimLeadingCharacter​(java.lang.String str,
                                                            char leadingCharacter)
        Trim all occurrences of the supplied leading character from the given String.
        Parameters:
        str - the String to check
        leadingCharacter - the leading character to be trimmed
        Returns:
        the trimmed String
      • trimTrailingCharacter

        public static java.lang.String trimTrailingCharacter​(java.lang.String str,
                                                             char trailingCharacter)
        Trim all occurrences of the supplied trailing character from the given String.
        Parameters:
        str - the String to check
        trailingCharacter - the trailing character to be trimmed
        Returns:
        the trimmed String
      • startsWithIgnoreCase

        public static boolean startsWithIgnoreCase​(java.lang.String str,
                                                   java.lang.String prefix)
        Test if the given String starts with the specified prefix, ignoring upper/lower case.
        Parameters:
        str - the String to check
        prefix - the prefix to look for
        Returns:
        true if the String starts with the prefix, case insensitive, or both null
        See Also:
        String.startsWith(java.lang.String, int)
      • endsWithIgnoreCase

        public static boolean endsWithIgnoreCase​(java.lang.String str,
                                                 java.lang.String suffix)
        Test if the given String ends with the specified suffix, ignoring upper/lower case.
        Parameters:
        str - the String to check
        suffix - the suffix to look for
        Returns:
        true if the String ends with the suffix, case insensitive, or both null
        See Also:
        String.endsWith(java.lang.String)
      • isEmpty

        public static boolean isEmpty​(java.lang.String str)
        Returns true if the given string is null or is the empty string.
        Parameters:
        str - a string reference to check
        Returns:
        true if the string is null or is the empty string
      • nullToEmpty

        public static java.lang.String nullToEmpty​(java.lang.String str)
        Returns the given string if it is non-null; the empty string otherwise.
        Parameters:
        str - the string to test and possibly return
        Returns:
        string itself if it is non-null; "" if it is null
      • emptyToNull

        public static java.lang.String emptyToNull​(java.lang.String str)
        Returns the given string if it is nonempty; null otherwise.
        Parameters:
        str - the string to test and possibly return
        Returns:
        string itself if it is nonempty; null if it is empty or null
      • startsWith

        public static boolean startsWith​(java.lang.String str,
                                         char prefix)
        Test if the given String starts with the specified prefix character.
        Parameters:
        str - the String to check
        prefix - the prefix character to look for
        Returns:
        true if the string starts with the specified prefix; otherwise false
        See Also:
        String.startsWith(java.lang.String, int)
      • endsWith

        public static boolean endsWith​(java.lang.String str,
                                       char suffix)
        Test if the given String ends with the specified prefix character.
        Parameters:
        str - the String to check
        suffix - the prefix character to look for
        Returns:
        true if the string ends with the specified suffix; otherwise false
        See Also:
        String.endsWith(java.lang.String)
      • replace

        public static java.lang.String replace​(java.lang.String str,
                                               java.lang.String search,
                                               java.lang.String replace)
        Replace all occurrences of a substring within a string with another string.
        Parameters:
        str - String to examine
        search - String to replace
        replace - String to insert
        Returns:
        a String with the replacements
      • replace

        public static java.lang.String replace​(java.lang.String str,
                                               java.lang.String[] search,
                                               java.lang.String[] replace)
        Replace all occurrences of a substring within a string with another string.
        Parameters:
        str - String to examine
        search - String array to replace
        replace - String array to insert
        Returns:
        a String with the replacements
      • split

        public static java.lang.String[] split​(java.lang.String str,
                                               java.lang.String delim)
        Returns an array of strings separated by the delimiter string.
        Parameters:
        str - the string to be separated
        delim - the delimiter
        Returns:
        an array, containing the splitted strings
      • split

        public static java.lang.String[] split​(java.lang.String str,
                                               java.lang.String delim,
                                               int size)
        Returns an array of strings separated by the delimiter string.
        Parameters:
        str - the string to be separated
        delim - the delimiter
        size - the size of the array
        Returns:
        an array, containing the splitted strings
      • split

        public static java.lang.String[] split​(java.lang.String str,
                                               char delim)
        Returns an array of strings separated by the delimiter string.
        Parameters:
        str - the string to be separated
        delim - the delimiter
        Returns:
        an array, containing the splitted strings
      • split

        public static java.lang.String[] split​(java.lang.String str,
                                               char delim,
                                               int size)
        Returns an array of strings separated by the delimiter string.
        Parameters:
        str - the string to be separated
        delim - the delimiter
        size - the size of the array
        Returns:
        an array, containing the splitted strings
      • search

        public static int search​(java.lang.String str,
                                 java.lang.String keyw)
        Returns the number of times the specified string was found in the target string, or 0 if there is no specified string.
        Parameters:
        str - the target string
        keyw - the string to find
        Returns:
        the number of times the specified string was found
      • searchIgnoreCase

        public static int searchIgnoreCase​(java.lang.String str,
                                           java.lang.String keyw)
        Returns the number of times the specified string was found in the target string, or 0 if there is no specified string. When searching for the specified string, it is not case-sensitive.
        Parameters:
        str - the target string
        keyw - the string to find
        Returns:
        the number of times the specified string was found
      • search

        public static int search​(java.lang.CharSequence chars,
                                 char c)
        Returns the number of times the specified character was found in the target string, or 0 if there is no specified character.
        Parameters:
        chars - the target string
        c - the character to find
        Returns:
        the number of times the specified character was found
      • searchIgnoreCase

        public static int searchIgnoreCase​(java.lang.CharSequence chars,
                                           char c)
        Returns the number of times the specified character was found in the target string, or 0 if there is no specified character. When searching for the specified character, it is not case-sensitive.
        Parameters:
        chars - the target string
        c - the character to find
        Returns:
        the number of times the specified character was found
      • tokenize

        public static java.lang.String[] tokenize​(java.lang.String str,
                                                  java.lang.String delimiters)
        Tokenize the given String into a String array via a StringTokenizer.
        Parameters:
        str - the String to tokenize
        delimiters - the delimiter characters
        Returns:
        an array of the tokens
      • tokenize

        public static java.lang.String[] tokenize​(java.lang.String str,
                                                  java.lang.String delimiters,
                                                  boolean trim)
        Tokenize the given String into a String array via a StringTokenizer.
        Parameters:
        str - the String to tokenize
        delimiters - the delimiter characters
        trim - trim the tokens via String's trim
        Returns:
        an array of the tokens
      • toDelimitedString

        public static java.lang.String toDelimitedString​(java.lang.Object[] arr,
                                                         java.lang.String delim)
        Convert a String array into a delimited String (e.g. CSV).

        Useful for toString() implementations.

        Parameters:
        arr - the array to display
        delim - the delimiter to use (typically a ",")
        Returns:
        the delimited String
      • toDelimitedString

        public static java.lang.String toDelimitedString​(java.util.Collection<?> list,
                                                         java.lang.String delim)
        Convert a Collection into a delimited String (e.g. CSV).

        Useful for toString() implementations.

        Parameters:
        list - the collection
        delim - the delimiter to use (typically a ",")
        Returns:
        the delimited String
      • toLineDelimitedString

        public static java.lang.String toLineDelimitedString​(java.lang.Object[] arr)
        Convert a String array into a delimited String by a system-dependent line separator.
        Parameters:
        arr - the array to display
        Returns:
        the delimited String
      • toLineDelimitedString

        public static java.lang.String toLineDelimitedString​(java.util.Collection<?> list)
        Convert a Collection into a delimited String by a system-dependent line separator.
        Parameters:
        list - the collection
        Returns:
        the delimited String
      • splitCommaDelimitedString

        public static java.lang.String[] splitCommaDelimitedString​(java.lang.String str)
        Convert a comma delimited list (e.g., a row from a CSV file) into an array of strings.
        Parameters:
        str - the input String
        Returns:
        an array of strings, or the empty array in case of empty input
      • joinCommaDelimitedList

        public static java.lang.String joinCommaDelimitedList​(java.lang.String[] arr)
        Convert a String array into a comma delimited String (i.e., CSV).
        Parameters:
        arr - the array to display
        Returns:
        the delimited String
      • joinCommaDelimitedList

        public static java.lang.String joinCommaDelimitedList​(java.util.Collection<?> list)
        Convert a Collection into a comma delimited String (i.e., CSV).
        Parameters:
        list - the collection
        Returns:
        the delimited String
      • repeat

        public static java.lang.String repeat​(char ch,
                                              int repeat)
        Returns padding using the specified delimiter repeated to a given length.
        Parameters:
        ch - character to repeat
        repeat - number of times to repeat char, negative treated as zero
        Returns:
        String with repeated character
      • parseLocaleString

        public static java.util.Locale parseLocaleString​(java.lang.String localeString)
        Parse the given localeString value into a Locale.

        This is the inverse operation of Locale's toString.

        Parameters:
        localeString - the locale String, following Locale's toString() format ("en", "en_UK", etc); also accepts spaces as separators, as an alternative to underscores
        Returns:
        a corresponding Locale instance
        Throws:
        java.lang.IllegalArgumentException - in case of an invalid locale specification
      • toLanguageTag

        public static java.lang.String toLanguageTag​(java.util.Locale locale)
        Determine the RFC 3066 compliant language tag, as used for the HTTP "Accept-Language" header.
        Parameters:
        locale - the Locale to transform to a language tag
        Returns:
        the RFC 3066 compliant language tag as String
      • parseTimeZoneString

        public static java.util.TimeZone parseTimeZoneString​(java.lang.String timeZoneString)
        Parse the given timeZoneString value into a TimeZone.
        Parameters:
        timeZoneString - the time zone String, following TimeZone.getTimeZone(String) but throwing IllegalArgumentException in case of an invalid time zone specification
        Returns:
        a corresponding TimeZone instance
        Throws:
        java.lang.IllegalArgumentException - in case of an invalid time zone specification
      • convertToHumanFriendlyByteSize

        public static java.lang.String convertToHumanFriendlyByteSize​(long size)
        Convert byte size into human friendly format.
        Parameters:
        size - the number of bytes
        Returns:
        a human friendly byte size (includes units)
      • convertToMachineFriendlyByteSize

        public static long convertToMachineFriendlyByteSize​(java.lang.String size)
        Convert byte size into machine friendly format.
        Parameters:
        size - the human friendly byte size (includes units)
        Returns:
        a number of bytes
        Throws:
        java.lang.NumberFormatException - if failed parse given size