Class StringUtils

java.lang.Object
com.alibaba.cloud.commons.lang.StringUtils

public final class StringUtils extends Object
StringUtils. copy from apache common-lang3.
Author:
theonefx
  • Field Details

    • EMPTY

      public static final String EMPTY
      The empty String "".
      Since:
      2.0
      See Also:
    • INDEX_NOT_FOUND

      public static final int INDEX_NOT_FOUND
      Represents a failed index search.
      Since:
      2.1
      See Also:
  • 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)
    • isNotEmpty

      public static boolean isNotEmpty(CharSequence cs)

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

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

      public static boolean isBlank(CharSequence cs)

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

       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
    • isNotBlank

      public static boolean isNotBlank(CharSequence cs)

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

      Whitespace is defined by Character.isWhitespace(char).

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

      public static String trim(String str)

      Removes control characters (char <= 32) from both ends of this String, handling null by returning null.

      The String is trimmed using String.trim(). Trim removes start and end characters <= 32.

       StringUtils.trim(null)          = null
       StringUtils.trim("")            = ""
       StringUtils.trim("     ")       = ""
       StringUtils.trim("abc")         = "abc"
       StringUtils.trim("    abc    ") = "abc"
       
      Parameters:
      str - the String to be trimmed, may be null
      Returns:
      the trimmed string, null if null String input
    • equals

      public static boolean equals(CharSequence cs1, CharSequence cs2)

      Compares two CharSequences, returning true if they represent equal sequences of characters.

      nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case sensitive.

       StringUtils.equals(null, null)   = true
       StringUtils.equals(null, "abc")  = false
       StringUtils.equals("abc", null)  = false
       StringUtils.equals("abc", "abc") = true
       StringUtils.equals("abc", "ABC") = false
       
      Parameters:
      cs1 - the first CharSequence, may be null
      cs2 - the second CharSequence, may be null
      Returns:
      true if the CharSequences are equal (case-sensitive), or both null
      See Also:
    • regionMatches

      public static boolean regionMatches(CharSequence cs, boolean ignoreCase, int thisStart, CharSequence substring, int start, int length)
      Green implementation of regionMatches.
      Parameters:
      cs - the CharSequence to be processed
      ignoreCase - whether or not to be case insensitive
      thisStart - the index to start on the cs CharSequence
      substring - the CharSequence to be looked for
      start - the index to start on the substring CharSequence
      length - character length of the region
      Returns:
      whether the region matched
    • substringAfter

      public static String substringAfter(String str, String separator)

      Gets the substring after the first occurrence of a separator. The separator is not returned.

      A null string input will return null. An empty ("") string input will return the empty string. A null separator will return the empty string if the input string is not null.

      If nothing is found, the empty string is returned.

       StringUtils.substringAfter(null, *)      = null
       StringUtils.substringAfter("", *)        = ""
       StringUtils.substringAfter(*, null)      = ""
       StringUtils.substringAfter("abc", "a")   = "bc"
       StringUtils.substringAfter("abcba", "b") = "cba"
       StringUtils.substringAfter("abc", "c")   = ""
       StringUtils.substringAfter("abc", "d")   = ""
       StringUtils.substringAfter("abc", "")    = "abc"
       
      Parameters:
      str - the String to get a substring from, may be null
      separator - the String to search for, may be null
      Returns:
      the substring after the first occurrence of the separator, null if null String input
      Since:
      2.0
    • substringBetween

      public static String substringBetween(String str, String tag)

      Gets the String that is nested in between two instances of the same String.

      A null input String returns null. A null tag returns null.

       StringUtils.substringBetween(null, *)            = null
       StringUtils.substringBetween("", "")             = ""
       StringUtils.substringBetween("", "tag")          = null
       StringUtils.substringBetween("tagabctag", null)  = null
       StringUtils.substringBetween("tagabctag", "")    = ""
       StringUtils.substringBetween("tagabctag", "tag") = "abc"
       
      Parameters:
      str - the String containing the substring, may be null
      tag - the String before and after the substring, may be null
      Returns:
      the substring, null if no match
      Since:
      2.0
    • substringBetween

      public static String substringBetween(String str, String open, String close)

      Gets the String that is nested in between two Strings. Only the first match is returned.

      A null input String returns null. A null open/close returns null (no match). An empty ("") open and close returns an empty string.

       StringUtils.substringBetween("wx[b]yz", "[", "]") = "b"
       StringUtils.substringBetween(null, *, *)          = null
       StringUtils.substringBetween(*, null, *)          = null
       StringUtils.substringBetween(*, *, null)          = null
       StringUtils.substringBetween("", "", "")          = ""
       StringUtils.substringBetween("", "", "]")         = null
       StringUtils.substringBetween("", "[", "]")        = null
       StringUtils.substringBetween("yabcz", "", "")     = ""
       StringUtils.substringBetween("yabcz", "y", "z")   = "abc"
       StringUtils.substringBetween("yabczyabcz", "y", "z")   = "abc"
       
      Parameters:
      str - the String containing the substring, may be null
      open - the String before the substring, may be null
      close - the String after the substring, may be null
      Returns:
      the substring, null if no match
      Since:
      2.0