Class StringUtils

java.lang.Object
io.github.projectunified.cronutils.utils.StringUtils

public class StringUtils extends Object

Operations on String that are null safe.

  • IsEmpty - checks if a String contains text
  • IndexOfAny - index-of any of a set of Strings

The StringUtils class defines certain words related to String handling.

  • null - null
  • empty - a zero-length string ("")
  • space - the space character (' ', char 32)
  • whitespace - the characters defined by Character.isWhitespace(char)
  • trim - the characters <= 32 as in String.trim()

StringUtils handles null input Strings quietly. That is to say that a null input will return null. Where a boolean or int is being returned details vary by method.

A side effect of the null handling is that a NullPointerException should be considered a bug in StringUtils.

Methods in this class give sample code to explain their operation. The symbol * is used to indicate any input including null.

#ThreadSafe#

Since:
1.0
Version:
$Id: StringUtils.java 1648067 2014-12-27 16:45:42Z britter $
See Also:
  • Field Details

  • Method Details

    • isEmpty

      public static boolean isEmpty(CharSequence cs)

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

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

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

      Parameters:
      cs - the CharSequence to check, may be null
      Returns:
      true if the CharSequence is empty or null
      Since:
      3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
    • containsAny

      public static boolean containsAny(CharSequence cs, char... searchChars)

      Checks if the CharSequence contains any character in the given. set of characters.

      A null CharSequence will return false. A null or zero length search array will return false.

       StringUtils.containsAny(null, *)                = false
       StringUtils.containsAny("", *)                  = false
       StringUtils.containsAny(*, null)                = false
       StringUtils.containsAny(*, [])                  = false
       StringUtils.containsAny("zzabyycdxx",['z','a']) = true
       StringUtils.containsAny("zzabyycdxx",['b','y']) = true
       StringUtils.containsAny("aba", ['z'])           = false
       
      Parameters:
      cs - the CharSequence to check, may be null
      searchChars - the chars to search for, may be null
      Returns:
      the true if any of the chars are found, false if no match or null input
      Since:
      2.4, 3.0 Changed signature from containsAny(String, char[]) to containsAny(CharSequence, char...)
    • isNumeric

      public static boolean isNumeric(CharSequence cs)

      Checks if the CharSequence contains only Unicode digits. A decimal point is not a Unicode digit and returns false.

      null will return false. An empty CharSequence (length()=0) will return false.

      Note that the method does not allow for a leading sign, either positive or negative. Also, if a String passes the numeric test, it may still generate a NumberFormatException when parsed by Integer.parseInt or Long.parseLong, e.g. if the value is outside the range for int or long respectively.

       StringUtils.isNumeric(null)   = false
       StringUtils.isNumeric("")     = false
       StringUtils.isNumeric("  ")   = false
       StringUtils.isNumeric("123")  = true
       StringUtils.isNumeric("१२३")  = true
       StringUtils.isNumeric("12 3") = false
       StringUtils.isNumeric("ab2c") = false
       StringUtils.isNumeric("12-3") = false
       StringUtils.isNumeric("12.3") = false
       StringUtils.isNumeric("-123") = false
       StringUtils.isNumeric("+123") = false
       
      Parameters:
      cs - the CharSequence to check, may be null
      Returns:
      true if only contains digits, and is non-null
      Since:
      3.0 Changed signature from isNumeric(String) to isNumeric(CharSequence), 3.0 Changed "" to return false and not true
    • join

      public static String join(Object[] parts, 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], *)              = "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"], ',')   = "null,,a"
       
      Parameters:
      parts - - 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