Class StringUtils


  • public final class StringUtils
    extends java.lang.Object
    Utility methods for Strings.
    Since:
    1.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String EMPTY_STRING
      Constant for an empty String.
      static java.lang.String[] EMPTY_STRING_ARRAY
      Constant for an empty String array.
      static java.lang.String FALSE
      Constant for the value false.
      static char SPACE
      a space.
      static java.lang.String TRUE
      Constant for the value true.
    • Constructor Summary

      Constructors 
      Constructor Description
      StringUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String capitalize​(java.lang.String str)
      Capitalizes the first character of the provided string.
      static java.lang.String convertDotToUnderscore​(java.lang.String dottedProperty)
      Replace the dots in the property with underscore and transform to uppercase.
      static java.lang.String convertDotToUnderscore​(java.lang.String dottedProperty, boolean uppercase)
      Replace the dots in the property with underscore and transform to uppercase based on given flag.
      static boolean hasText​(java.lang.CharSequence str)
      Return whether the given string has non whitespace characters.
      static java.util.List<java.lang.String> internListOf​(java.lang.Object... objects)
      Converts the given objects into a set of interned strings contained within an internal pool of sets.
      static java.util.Map<java.lang.String,​java.lang.Object> internMapOf​(java.lang.Object... values)
      Converts the given objects into a map of interned strings.
      static boolean isDigits​(java.lang.String str)
      Is the given string a series of digits.
      static boolean isEmpty​(java.lang.CharSequence str)
      Return whether the given string is empty.
      static boolean isNotEmpty​(java.lang.CharSequence str)
      Return whether the given string is not empty.
      static boolean isTrue​(java.lang.String booleanString)
      Is the boolean string true.
      static java.util.Locale parseLocale​(java.lang.String localeValue)
      Parse the given String value into a Locale, accepting the Locale.toString() format as well as BCP 47 language tags.
      static java.lang.String prependUri​(java.lang.String baseUri, java.lang.String uri)
      Prepends a partial uri and normalizes / characters.
      static java.lang.Iterable<java.lang.String> splitOmitEmptyStrings​(java.lang.CharSequence sequence, char splitCharacter)
      Fast split by one character iterable.
      static java.util.Iterator<java.lang.String> splitOmitEmptyStringsIterator​(java.lang.CharSequence sequence, char splitCharacter)
      Fast split by one character iterator.
      static java.util.List<java.lang.String> splitOmitEmptyStringsList​(java.lang.CharSequence sequence, char splitCharacter)
      Fast split by one character iterable.
      static java.lang.String[] tokenizeToStringArray​(java.lang.String str, java.lang.String delimiters)
      Tokenize the given String into a String array via a StringTokenizer.
      static java.lang.String[] tokenizeToStringArray​(java.lang.String str, java.lang.String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)
      Tokenize the given String into a String array via a StringTokenizer.
      static java.lang.String trimLeading​(java.lang.String str, java.util.function.Predicate<java.lang.Character> predicate)
      Returns a new string without any leading characters that match the supplied predicate.
      static java.lang.String trimLeadingCharacter​(java.lang.String str, char c)
      Returns a new string without any leading characters that match the supplied character.
      static java.lang.String trimLeadingWhitespace​(java.lang.String str)
      Returns a new string without any leading whitespace.
      static java.lang.String trimToNull​(java.lang.String string)
      Trims the supplied string.
      • Methods inherited from class java.lang.Object

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

      • TRUE

        public static final java.lang.String TRUE
        Constant for the value true.
        See Also:
        Constant Field Values
      • FALSE

        public static final java.lang.String FALSE
        Constant for the value false.
        See Also:
        Constant Field Values
      • EMPTY_STRING_ARRAY

        public static final java.lang.String[] EMPTY_STRING_ARRAY
        Constant for an empty String array.
      • EMPTY_STRING

        public static final java.lang.String EMPTY_STRING
        Constant for an empty String.
        See Also:
        Constant Field Values
    • Constructor Detail

      • StringUtils

        public StringUtils()
    • Method Detail

      • isEmpty

        public static boolean isEmpty​(@Nullable
                                      java.lang.CharSequence str)
        Return whether the given string is empty.
        Parameters:
        str - The string
        Returns:
        True if str is empty or null
      • isNotEmpty

        public static boolean isNotEmpty​(@Nullable
                                         java.lang.CharSequence str)
        Return whether the given string is not empty.
        Parameters:
        str - The string
        Returns:
        True if str is not null and not empty
      • hasText

        public static boolean hasText​(@Nullable
                                      java.lang.CharSequence str)
        Return whether the given string has non whitespace characters.
        Parameters:
        str - The string
        Returns:
        True if str contains any non whitespace characters
      • internListOf

        public static java.util.List<java.lang.String> internListOf​(java.lang.Object... objects)
        Converts the given objects into a set of interned strings contained within an internal pool of sets. See String.intern().
        Parameters:
        objects - The objects
        Returns:
        A unmodifiable, pooled set of strings
      • internMapOf

        public static java.util.Map<java.lang.String,​java.lang.Object> internMapOf​(java.lang.Object... values)
        Converts the given objects into a map of interned strings. See String.intern().
        Parameters:
        values - The objects
        Returns:
        An unmodifiable set of strings
        See Also:
        CollectionUtils.mapOf(Object...)
      • isDigits

        public static boolean isDigits​(java.lang.String str)
        Is the given string a series of digits.
        Parameters:
        str - The string
        Returns:
        True if it is a series of digits
      • parseLocale

        @Nullable
        public static java.util.Locale parseLocale​(java.lang.String localeValue)
        Parse the given String value into a Locale, accepting the Locale.toString() format as well as BCP 47 language tags.
        Parameters:
        localeValue - the locale value: following either Locale's toString() format ("en", "en_UK", etc), also accepting spaces as separators (as an alternative to underscores), or BCP 47 (e.g. "en-UK") as specified by Locale.forLanguageTag(java.lang.String) on Java 7+

        Copied from the Spring Framework while retaining all license, copyright and author information.

        Returns:
        a corresponding Locale instance, or null if none
        Throws:
        java.lang.IllegalArgumentException - in case of an invalid locale specification
        Since:
        2.3.0
        See Also:
        Locale.forLanguageTag(java.lang.String)
      • trimLeadingWhitespace

        public static java.lang.String trimLeadingWhitespace​(java.lang.String str)
        Returns a new string without any leading whitespace.
        Parameters:
        str - The string
        Returns:
        The string without leading whitespace
        Since:
        2.3.0
      • trimLeadingCharacter

        public static java.lang.String trimLeadingCharacter​(java.lang.String str,
                                                            char c)
        Returns a new string without any leading characters that match the supplied character.
        Parameters:
        str - The string
        c - The character to remove
        Returns:
        The string without leading characters matching the supplied character.
        Since:
        2.3.0
      • trimLeading

        public static java.lang.String trimLeading​(java.lang.String str,
                                                   java.util.function.Predicate<java.lang.Character> predicate)
        Returns a new string without any leading characters that match the supplied predicate.
        Parameters:
        str - The string
        predicate - The predicate to test characters against
        Returns:
        The string without leading characters matching the supplied predicate.
        Since:
        2.3.0
      • tokenizeToStringArray

        public static java.lang.String[] tokenizeToStringArray​(java.lang.String str,
                                                               java.lang.String delimiters)
        Tokenize the given String into a String array via a StringTokenizer. Trims tokens and omits empty tokens.

        The given delimiters string is supposed to consist of any number of delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single character; for multi-character delimiters, consider using delimitedListToStringArray

        Copied from the Spring Framework while retaining all license, copyright and author information.

        Parameters:
        str - the String to tokenize
        delimiters - the delimiter characters, assembled as String (each of those characters is individually considered as delimiter).
        Returns:
        an array of the tokens
        See Also:
        StringTokenizer, String.trim()
      • tokenizeToStringArray

        public static java.lang.String[] tokenizeToStringArray​(java.lang.String str,
                                                               java.lang.String delimiters,
                                                               boolean trimTokens,
                                                               boolean ignoreEmptyTokens)
        Tokenize the given String into a String array via a StringTokenizer.

        The given delimiters string is supposed to consist of any number of delimiter characters. Each of those characters can be used to separate tokens. A delimiter is always a single character; for multi-character delimiters, consider using delimitedListToStringArray

        Copied from the Spring Framework while retaining all license, copyright and author information.

        Parameters:
        str - the String to tokenize
        delimiters - the delimiter characters, assembled as String (each of those characters is individually considered as delimiter)
        trimTokens - trim the tokens via String's trim
        ignoreEmptyTokens - omit empty tokens from the result array (only applies to tokens that are empty after trimming; StringTokenizer will not consider subsequent delimiters as token in the first place).
        Returns:
        an array of the tokens (null if the input String was null)
        See Also:
        StringTokenizer, String.trim()
      • convertDotToUnderscore

        public static java.lang.String convertDotToUnderscore​(java.lang.String dottedProperty)
        Replace the dots in the property with underscore and transform to uppercase.
        Parameters:
        dottedProperty - The property with dots, example - a.b.c
        Returns:
        The converted value
      • convertDotToUnderscore

        public static java.lang.String convertDotToUnderscore​(java.lang.String dottedProperty,
                                                              boolean uppercase)
        Replace the dots in the property with underscore and transform to uppercase based on given flag.
        Parameters:
        dottedProperty - The property with dots, example - a.b.c
        uppercase - To transform to uppercase string
        Returns:
        The converted value
      • prependUri

        public static java.lang.String prependUri​(java.lang.String baseUri,
                                                  java.lang.String uri)
        Prepends a partial uri and normalizes / characters. For example, if the base uri is "/foo/" and the uri is "/bar/", the output will be "/foo/bar/". Similarly if the base uri is "/foo" and the uri is "bar", the output will be "/foo/bar"
        Parameters:
        baseUri - The uri to prepend. Eg. /foo
        uri - The uri to combine with the baseUri. Eg. /bar
        Returns:
        A combined uri string
      • capitalize

        public static java.lang.String capitalize​(java.lang.String str)
        Capitalizes the first character of the provided string.
        Parameters:
        str - The string to capitalize
        Returns:
        The capitalized string
      • trimToNull

        @Nullable
        public static java.lang.String trimToNull​(@Nullable
                                                  java.lang.String string)
        Trims the supplied string. If the string is empty or null before or after trimming, null is returned.
        Parameters:
        string - the string to trim
        Returns:
        the trimmed string or null
      • isTrue

        public static boolean isTrue​(java.lang.String booleanString)
        Is the boolean string true. Values that represent true are: yes, y, on, and true.
        Parameters:
        booleanString - The boolean string
        Returns:
        True if it is a valid value
      • splitOmitEmptyStrings

        public static java.lang.Iterable<java.lang.String> splitOmitEmptyStrings​(java.lang.CharSequence sequence,
                                                                                 char splitCharacter)
        Fast split by one character iterable. Implementation omits empty strings. This should be a fast alternative to String.split(String) where it's used with one character.
        Parameters:
        sequence - The sequence to split
        splitCharacter - The split character
        Returns:
        The iterable of possible sequences
        Since:
        2.5.0
      • splitOmitEmptyStringsList

        public static java.util.List<java.lang.String> splitOmitEmptyStringsList​(java.lang.CharSequence sequence,
                                                                                 char splitCharacter)
        Fast split by one character iterable. Implementation omits empty strings. This should be a fast alternative to String.split(String) where it's used with one character.
        Parameters:
        sequence - The sequence to split
        splitCharacter - The split character
        Returns:
        The list of possible sequences
        Since:
        2.5.0
      • splitOmitEmptyStringsIterator

        public static java.util.Iterator<java.lang.String> splitOmitEmptyStringsIterator​(java.lang.CharSequence sequence,
                                                                                         char splitCharacter)
        Fast split by one character iterator. Implementation omits empty strings. This should be a fast alternative to String.split(String) where it's used with one character.
        Parameters:
        sequence - The sequence to split
        splitCharacter - The split character
        Returns:
        The iterator of possible sequences
        Since:
        2.5.0