Class StringUtils


  • public class StringUtils
    extends Object
    Clone from org.apache.commons:commons-lang3:3.11.
    Author:
    matthias.drummer
    • Method Detail

      • defaultString

        public static String defaultString​(String str)

        Returns either the passed in String, or if the String is null, an empty String ("").

         StringUtils.defaultString(null)  = ""
         StringUtils.defaultString("")    = ""
         StringUtils.defaultString("bat") = "bat"
         
        Parameters:
        str - the String to check, may be null
        Returns:
        the passed in String, or the empty String if it was null
        See Also:
        String.valueOf(Object)
      • defaultString

        public static String defaultString​(String str,
                                           String defaultStr)

        Returns either the passed in String, or if the String is null, the value of defaultStr.

         StringUtils.defaultString(null, "NULL")  = "NULL"
         StringUtils.defaultString("", "NULL")    = ""
         StringUtils.defaultString("bat", "NULL") = "bat"
         
        Parameters:
        str - the String to check, may be null
        defaultStr - the default String to return if the input is null, may be null
        Returns:
        the passed in String, or the default if it was null
        See Also:
        String.valueOf(Object)
      • join

        public static String join​(Iterable<?> iterable,
                                  String 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. A null separator is the same as an empty String ("").

        See the examples here: join(Object[], String).

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

        public static String join​(Iterator<?> iterator,
                                  String 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. A null separator is the same as an empty String ("").

        See the examples here: join(Object[], String).

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

        public static String join​(Object[] array,
                                  String separator)

        Joins the elements of the provided array into a single String containing the provided list of elements.

        No delimiter is added before or after the list. A null separator is the same as an empty String (""). Null objects or empty strings within the array are represented by empty strings.

         StringUtils.join(null, *)                = null
         StringUtils.join([], *)                  = ""
         StringUtils.join([null], *)              = ""
         StringUtils.join(["a", "b", "c"], "--")  = "a--b--c"
         StringUtils.join(["a", "b", "c"], null)  = "abc"
         StringUtils.join(["a", "b", "c"], "")    = "abc"
         StringUtils.join([null, "", "a"], ',')   = ",,a"
         
        Parameters:
        array - the array of values to join together, may be null
        separator - the separator character to use, null treated as ""
        Returns:
        the joined String, null if null array input
      • join

        public static String join​(Object[] array,
                                  String separator,
                                  int startIndex,
                                  int endIndex)

        Joins the elements of the provided array into a single String containing the provided list of elements.

        No delimiter is added before or after the list. A null separator is the same as an empty String (""). Null objects or empty strings within the array are represented by empty strings.

         StringUtils.join(null, *, *, *)                = null
         StringUtils.join([], *, *, *)                  = ""
         StringUtils.join([null], *, *, *)              = ""
         StringUtils.join(["a", "b", "c"], "--", 0, 3)  = "a--b--c"
         StringUtils.join(["a", "b", "c"], "--", 1, 3)  = "b--c"
         StringUtils.join(["a", "b", "c"], "--", 2, 3)  = "c"
         StringUtils.join(["a", "b", "c"], "--", 2, 2)  = ""
         StringUtils.join(["a", "b", "c"], null, 0, 3)  = "abc"
         StringUtils.join(["a", "b", "c"], "", 0, 3)    = "abc"
         StringUtils.join([null, "", "a"], ',', 0, 3)   = ",,a"
         
        Parameters:
        array - the array of values to join together, may be null
        separator - the separator character to use, null treated as ""
        startIndex - the first index to start joining from.
        endIndex - the index to stop joining from (exclusive).
        Returns:
        the joined String, null if null array input; or the empty string if endIndex - startIndex <= 0. The number of joined entries is given by endIndex - startIndex
        Throws:
        ArrayIndexOutOfBoundsException - ife
        startIndex < 0 or
        startIndex >= array.length() or
        endIndex < 0 or
        endIndex > array.length()
      • isBlank

        public static boolean isBlank​(CharSequence cs)

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

        Whitespace is defined by Character.isWhitespace(char).

         StringUtils.isBlank(null)      = true
         StringUtils.isBlank("")        = true
         StringUtils.isBlank(" ")       = true
         StringUtils.isBlank("bob")     = false
         StringUtils.isBlank("  bob  ") = false
         
        Parameters:
        cs - the CharSequence to check, may be null
        Returns:
        true if the CharSequence is null, empty or whitespace only
        Since:
        2.0, 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
      • length

        public static int length​(CharSequence cs)
        Gets a CharSequence length or 0 if the CharSequence is null.
        Parameters:
        cs - a CharSequence or null
        Returns:
        CharSequence length or 0 if the CharSequence is null.
        Since:
        2.4, 3.0 Changed signature from length(String) to length(CharSequence)