Class StringValidator

java.lang.Object
org.miaixz.bus.core.text.StringValidator
Direct Known Subclasses:
CharsBacker

public class StringValidator extends Object
字符串检查工具类,提供字符串的blank和empty等检查
  • empty定义:null or 空字符串:""
  • blank定义:null or 空字符串:"" or 空格、全角空格、制表符、换行符,等不可见字符
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • StringValidator

      public StringValidator()
  • Method Details

    • isBlank

      public static boolean isBlank(CharSequence text)

      字符串是否为空白,空白的定义如下:

      1. null
      2. 空字符串:""
      3. 空格、全角空格、制表符、换行符,等不可见字符

      例:

      • StringKit.isBlank(null) // true
      • StringKit.isBlank("") // true
      • StringKit.isBlank(" \t\n") // true
      • StringKit.isBlank("abc") // false

      注意:该方法与 isEmpty(CharSequence) 的区别是: 该方法会校验空白字符,且性能相对于 isEmpty(CharSequence) 略慢。

      建议:

      Parameters:
      text - 被检测的字符串
      Returns:
      若为空白,则返回 true
      See Also:
    • isNotBlank

      public static boolean isNotBlank(CharSequence text)
      字符串是否为非空白,非空白的定义如下:
      1. 不为 null
      2. 不为空字符串:""
      3. 不为空格、全角空格、制表符、换行符,等不可见字符

      例:

      • StringKit.isNotBlank(null) // false
      • StringKit.isNotBlank("") // false
      • StringKit.isNotBlank(" \t\n") // false
      • StringKit.isNotBlank("abc") // true

      注意:该方法与 isNotEmpty(CharSequence) 的区别是: 该方法会校验空白字符,且性能相对于 isNotEmpty(CharSequence) 略慢。

      建议:仅对于客户端(或第三方接口)传入的参数使用该方法。

      Parameters:
      text - 被检测的字符串
      Returns:
      是否为非空
      See Also:
    • isEmpty

      public static boolean isEmpty(CharSequence text)

      字符串是否为空,空的定义如下:

      1. null
      2. 空字符串:""

      例:

      • StringKit.isEmpty(null) // true
      • StringKit.isEmpty("") // true
      • StringKit.isEmpty(" \t\n") // false
      • StringKit.isEmpty("abc") // false

      注意:该方法与 isBlank(CharSequence) 的区别是:该方法不校验空白字符。

      建议:

      Parameters:
      text - 被检测的字符串
      Returns:
      是否为空
      See Also:
    • isNotEmpty

      public static boolean isNotEmpty(CharSequence text)

      字符串是否为非空白,非空白的定义如下:

      1. 不为 null
      2. 不为空字符串:""

      例:

      • StringKit.isNotEmpty(null) // false
      • StringKit.isNotEmpty("") // false
      • StringKit.isNotEmpty(" \t\n") // true
      • StringKit.isNotEmpty("abc") // true

      注意:该方法与 isNotBlank(CharSequence) 的区别是:该方法不校验空白字符。

      建议:该方法建议用于工具类或任何可以预期的方法参数的校验中。

      Parameters:
      text - 被检测的字符串
      Returns:
      是否为非空
      See Also:
    • hasEmpty

      public static boolean hasEmpty(CharSequence... args)

      是否包含空字符串。

      如果指定的字符串数组的长度为 0,或者其中的任意一个元素是空字符串,则返回 true。

      例:

      • StringKit.hasEmpty() // true
      • StringKit.hasEmpty("", null) // true
      • StringKit.hasEmpty("123", "") // true
      • StringKit.hasEmpty("123", "abc") // false
      • StringKit.hasEmpty(" ", "\t", "\n") // false

      注意:该方法与 isAllEmpty(CharSequence...) 的区别在于:

      • hasEmpty(CharSequence...) 等价于 isEmpty(...) || isEmpty(...) || ...
      • isAllEmpty(CharSequence...) 等价于 isEmpty(...) && isEmpty(...) && ...
      Parameters:
      args - 字符串列表
      Returns:
      是否包含空字符串
    • hasEmpty

      public static boolean hasEmpty(Iterable<? extends CharSequence> args)

      是否包含空字符串。

      如果指定的字符串数组的长度为 0,或者其中的任意一个元素是空字符串,则返回 true。

      Parameters:
      args - 字符串列表
      Returns:
      是否包含空字符串
    • isAllEmpty

      public static boolean isAllEmpty(CharSequence... args)

      指定字符串数组中的元素,是否全部为空字符串。

      如果指定的字符串数组的长度为 0,或者所有元素都是空字符串,则返回 true。

      例:

      • StringKit.isAllEmpty() // true
      • StringKit.isAllEmpty("", null) // true
      • StringKit.isAllEmpty("123", "") // false
      • StringKit.isAllEmpty("123", "abc") // false
      • StringKit.isAllEmpty(" ", "\t", "\n") // false

      注意:该方法与 hasEmpty(CharSequence...) 的区别在于:

      • hasEmpty(CharSequence...) 等价于 isEmpty(...) || isEmpty(...) || ...
      • isAllEmpty(CharSequence...) 等价于 isEmpty(...) && isEmpty(...) && ...
      Parameters:
      args - 字符串列表
      Returns:
      所有字符串是否为空白
    • isAllEmpty

      public static boolean isAllEmpty(Iterable<? extends CharSequence> args)

      指定字符串数组中的元素,是否全部为空字符串。

      如果指定的字符串数组的长度为 0,或者所有元素都是空字符串,则返回 true。

      Parameters:
      args - 字符串列表
      Returns:
      所有字符串是否为空白
    • isAllNotEmpty

      public static boolean isAllNotEmpty(CharSequence... args)

      指定字符串数组中的元素,是否都不为空字符串。

      如果指定的字符串数组的长度不为 0,或者所有元素都不是空字符串,则返回 true。

      例:

      • StringKit.isAllNotEmpty() // false
      • StringKit.isAllNotEmpty("", null) // false
      • StringKit.isAllNotEmpty("123", "") // false
      • StringKit.isAllNotEmpty("123", "abc") // true
      • StringKit.isAllNotEmpty(" ", "\t", "\n") // true

      注意:该方法与 isAllEmpty(CharSequence...) 的区别在于:

      • isAllEmpty(CharSequence...) 等价于 isEmpty(...) && isEmpty(...) && ...
      • isAllNotEmpty(CharSequence...) 等价于 !isEmpty(...) && !isEmpty(...) && ...
      Parameters:
      args - 字符串数组
      Returns:
      所有字符串是否都不为为空白
    • isNullOrUndefined

      public static boolean isNullOrUndefined(CharSequence text)
      检查字符串是否为null、“null”、“undefined”
      Parameters:
      text - 被检查的字符串
      Returns:
      是否为null、“null”、“undefined”
    • isEmptyOrUndefined

      public static boolean isEmptyOrUndefined(CharSequence text)
      检查字符串是否为null、“”、“null”、“undefined”
      Parameters:
      text - 被检查的字符串
      Returns:
      是否为null、“”、“null”、“undefined”
    • isBlankOrUndefined

      public static boolean isBlankOrUndefined(CharSequence text)
      检查字符串是否为null、空白串、“null”、“undefined”
      Parameters:
      text - 被检查的字符串
      Returns:
      是否为null、空白串、“null”、“undefined”
    • isAllCharMatch

      public static boolean isAllCharMatch(CharSequence value, Predicate<Character> matcher)
      字符串的每一个字符是否都与定义的匹配器匹配
      Parameters:
      value - 字符串
      matcher - 匹配器
      Returns:
      是否全部匹配