Class Strings


  • public final class Strings
    extends java.lang.Object
    Consider this class private.
    See Also:
    Apache Commons Lang
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String COMMA_DELIMITED_RE  
      static java.lang.String EMPTY
      The empty string.
      static java.lang.String[] EMPTY_ARRAY
      The empty array.
      static java.lang.String LINE_SEPARATOR
      OS-dependent line separator, defaults to "\n" if the system property ""line.separator" cannot be read.
      private static java.lang.ThreadLocal<java.lang.StringBuilder> tempStr  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Strings()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String concat​(java.lang.String str1, java.lang.String str2)
      Concatenates 2 Strings without allocation.
      static java.lang.String dquote​(java.lang.String str)
      Returns a double quoted string.
      static boolean isBlank​(java.lang.String s)
      Checks if a String is blank.
      static boolean isEmpty​(java.lang.CharSequence cs)
      Checks if a CharSequence is empty ("") or null.
      static boolean isNotBlank​(java.lang.String s)
      Checks if a String is not blank.
      static boolean isNotEmpty​(java.lang.CharSequence cs)
      Checks if a CharSequence is not empty ("") and not null.
      static java.lang.String join​(java.lang.Iterable<?> iterable, char separator)
      Joins the elements of the provided Iterable into a single String containing the provided elements.
      static java.lang.String join​(java.util.Iterator<?> iterator, char separator)
      Joins the elements of the provided Iterator into a single String containing the provided elements.
      static java.lang.String left​(java.lang.String str, int len)
      Gets the leftmost len characters of a String.
      static java.lang.String quote​(java.lang.String str)
      Returns a quoted string.
      static java.lang.String repeat​(java.lang.String str, int count)
      Creates a new string repeating given str count times.
      static java.lang.String[] splitList​(java.lang.String string)  
      static java.lang.String toRootLowerCase​(java.lang.String str)
      Shorthand for str.toLowerCase(Locale.ROOT);
      static java.lang.String toRootUpperCase​(java.lang.String str)
      Shorthand for str.toUpperCase(Locale.ROOT);
      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.
      • Methods inherited from class java.lang.Object

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

      • tempStr

        private static final java.lang.ThreadLocal<java.lang.StringBuilder> tempStr
      • COMMA_DELIMITED_RE

        private static final java.lang.String COMMA_DELIMITED_RE
        See Also:
        Constant Field Values
      • EMPTY_ARRAY

        public static final java.lang.String[] EMPTY_ARRAY
        The empty array.
      • LINE_SEPARATOR

        public static final java.lang.String LINE_SEPARATOR
        OS-dependent line separator, defaults to "\n" if the system property ""line.separator" cannot be read.
    • Constructor Detail

      • Strings

        private Strings()
    • Method Detail

      • dquote

        public static java.lang.String dquote​(java.lang.String str)
        Returns a double quoted string.
        Parameters:
        str - a String
        Returns:
        "str"
      • isBlank

        public static boolean isBlank​(java.lang.String s)
        Checks if a String is blank. A blank string is one that is either null, empty, or all characters are Character.isWhitespace(char).
        Parameters:
        s - the String to check, may be null
        Returns:
        true if the String is null, empty, or all characters are Character.isWhitespace(char)
      • isEmpty

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

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

         Strings.isEmpty(null)      = true
         Strings.isEmpty("")        = true
         Strings.isEmpty(" ")       = false
         Strings.isEmpty("bob")     = false
         Strings.isEmpty("  bob  ") = false
         

        NOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is available in isBlank().

        Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isEmpty(CharSequence)

        Parameters:
        cs - the CharSequence to check, may be null
        Returns:
        true if the CharSequence is empty or null
      • isNotBlank

        public static boolean isNotBlank​(java.lang.String s)
        Checks if a String is not blank. The opposite of isBlank(String).
        Parameters:
        s - the String to check, may be null
        Returns:
        true if the String is non-null and has content after being trimmed.
      • isNotEmpty

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

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

         Strings.isNotEmpty(null)      = false
         Strings.isNotEmpty("")        = false
         Strings.isNotEmpty(" ")       = true
         Strings.isNotEmpty("bob")     = true
         Strings.isNotEmpty("  bob  ") = true
         

        Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isNotEmpty(CharSequence)

        Parameters:
        cs - the CharSequence to check, may be null
        Returns:
        true if the CharSequence is not empty and not null
      • join

        public static java.lang.String join​(java.lang.Iterable<?> iterable,
                                            char separator)

        Joins the elements of the provided Iterable into a single String containing the provided elements.

        No delimiter is added before or after the list. Null objects or empty strings within the iteration are represented by empty strings.

        Parameters:
        iterable - the Iterable providing the values to join together, may be null
        separator - the separator character to use
        Returns:
        the joined String, null if null iterator input
      • join

        public static java.lang.String join​(java.util.Iterator<?> iterator,
                                            char separator)

        Joins the elements of the provided Iterator into a single String containing the provided elements.

        No delimiter is added before or after the list. Null objects or empty strings within the iteration are represented by empty strings.

        Parameters:
        iterator - the Iterator of values to join together, may be null
        separator - the separator character to use
        Returns:
        the joined String, null if null iterator input
      • splitList

        public static java.lang.String[] splitList​(java.lang.String string)
      • left

        public static java.lang.String left​(java.lang.String str,
                                            int len)

        Gets the leftmost len characters of a String.

        If len characters are not available, or the String is null, the String will be returned without an exception. An empty String is returned if len is negative.

         StringUtils.left(null, *)    = null
         StringUtils.left(*, -ve)     = ""
         StringUtils.left("", *)      = ""
         StringUtils.left("abc", 0)   = ""
         StringUtils.left("abc", 2)   = "ab"
         StringUtils.left("abc", 4)   = "abc"
         

        Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.

        Parameters:
        str - the String to get the leftmost characters from, may be null
        len - the length of the required String
        Returns:
        the leftmost characters, null if null String input
      • quote

        public static java.lang.String quote​(java.lang.String str)
        Returns a quoted string.
        Parameters:
        str - a String
        Returns:
        'str'
      • 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.

         Strings.trimToNull(null)          = null
         Strings.trimToNull("")            = null
         Strings.trimToNull("     ")       = null
         Strings.trimToNull("abc")         = "abc"
         Strings.trimToNull("    abc    ") = "abc"
         

        Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.trimToNull(String)

        Parameters:
        str - the String to be trimmed, may be null
        Returns:
        the trimmed String, null if only chars <= 32, empty or null String input
      • toRootLowerCase

        public static java.lang.String toRootLowerCase​(java.lang.String str)
        Shorthand for str.toLowerCase(Locale.ROOT);
        Parameters:
        str - The string to upper case.
        Returns:
        a new string
        See Also:
        String.toLowerCase(Locale)
      • toRootUpperCase

        public static java.lang.String toRootUpperCase​(java.lang.String str)
        Shorthand for str.toUpperCase(Locale.ROOT);
        Parameters:
        str - The string to lower case.
        Returns:
        a new string
        See Also:
        String.toLowerCase(Locale)
      • concat

        public static java.lang.String concat​(java.lang.String str1,
                                              java.lang.String str2)
        Concatenates 2 Strings without allocation.
        Parameters:
        str1 - the first string.
        str2 - the second string.
        Returns:
        the concatenated String.
      • repeat

        public static java.lang.String repeat​(java.lang.String str,
                                              int count)
        Creates a new string repeating given str count times.
        Parameters:
        str - input string
        count - the repetition count
        Returns:
        the new string
        Throws:
        java.lang.IllegalArgumentException - if either str is null or count is negative