Java
除了isEmpty/isNotEmpty/isNotBlank/isBlank外,还有isAnyEmpty/isNoneEmpty/isAnyBlank/isNoneBlank的存在,来探索org.apache.commons.lang3.StringUtils;这个工具类。

isEmpty系列

StringUtils.isEmpty()

是否为空。可以看到 " " 空格是会绕过这种空判断,因为是一个空格,并不是严格的空值,会导致 isEmpty(" ")=false

  1. StringUtils.isEmpty(null) = true
  2. StringUtils.isEmpty("") = true
  3. StringUtils.isEmpty(" ") = false
  4. StringUtils.isEmpty("bob") = false
  5. StringUtils.isEmpty(" bob ") = false
  6. /**
  7. *
  8. * <p>NOTE: This method changed in Lang version 2.0.
  9. * It no longer trims the CharSequence.
  10. * That functionality is available in isBlank().</p>
  11. *
  12. * @param cs the CharSequence to check, may be null
  13. * @return {@code true} if the CharSequence is empty or null
  14. * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
  15. */
  16. public static boolean isEmpty(final CharSequence cs) {
  17. return cs == null || cs.length() == 0;
  18. }

StringUtils.isNotEmpty()

相当于不为空,= !isEmpty()

  1. public static boolean isNotEmpty(final CharSequence cs) {
  2. return !isEmpty(cs);
  3. }

StringUtils.isAnyEmpty()

是否有一个为空,只有一个为空,就为true

  1. StringUtils.isAnyEmpty(null) = true
  2. StringUtils.isAnyEmpty(null, "foo") = true
  3. StringUtils.isAnyEmpty("", "bar") = true
  4. StringUtils.isAnyEmpty("bob", "") = true
  5. StringUtils.isAnyEmpty(" bob ", null) = true
  6. StringUtils.isAnyEmpty(" ", "bar") = false
  7. StringUtils.isAnyEmpty("foo", "bar") = false
  8. /**
  9. * @param css the CharSequences to check, may be null or empty
  10. * @return {@code true} if any of the CharSequences are empty or null
  11. * @since 3.2
  12. */
  13. public static boolean isAnyEmpty(final CharSequence... css) {
  14. if (ArrayUtils.isEmpty(css)) {
  15. return true;
  16. }
  17. for (final CharSequence cs : css){
  18. if (isEmpty(cs)) {
  19. return true;
  20. }
  21. }
  22. return false;
  23. }

StringUtils.isNoneEmpty()

相当于!isAnyEmpty(css),必须所有的值都不为空才返回true

  1. /**
  2. * <p>Checks if none of the CharSequences are empty ("") or null.</p>
  3. *
  4. * <pre>
  5. * StringUtils.isNoneEmpty(null) = false
  6. * StringUtils.isNoneEmpty(null, "foo") = false
  7. * StringUtils.isNoneEmpty("", "bar") = false
  8. * StringUtils.isNoneEmpty("bob", "") = false
  9. * StringUtils.isNoneEmpty(" bob ", null) = false
  10. * StringUtils.isNoneEmpty(" ", "bar") = true
  11. * StringUtils.isNoneEmpty("foo", "bar") = true
  12. * </pre>
  13. *
  14. * @param css the CharSequences to check, may be null or empty
  15. * @return {@code true} if none of the CharSequences are empty or null
  16. * @since 3.2
  17. */
  18. public static boolean isNoneEmpty(final CharSequence... css) {}

isBank系列

StringUtils.isBlank()

是否为真空值(空格或者空值)

  1. StringUtils.isBlank(null) = true
  2. StringUtils.isBlank("") = true
  3. StringUtils.isBlank(" ") = true
  4. StringUtils.isBlank("bob") = false
  5. StringUtils.isBlank(" bob ") = false
  6. /**
  7. * <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
  8. * @param cs the CharSequence to check, may be null
  9. * @return {@code true} if the CharSequence is null, empty or whitespace
  10. * @since 2.0
  11. * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
  12. */
  13. public static boolean isBlank(final CharSequence cs) {
  14. int strLen;
  15. if (cs == null || (strLen = cs.length()) == 0) {
  16. return true;
  17. }
  18. for (int i = 0; i < strLen; i++) {
  19. if (Character.isWhitespace(cs.charAt(i)) == false) {
  20. return false;
  21. }
  22. }
  23. return true;
  24. }

StringUtils.isNotBlank()

是否真的不为空,不是空格或者空值,相当于!isBlank();

  1. public static boolean isNotBlank(final CharSequence cs) {
  2. return !isBlank(cs);
  3. }

StringUtils.isAnyBlank()

是否包含任何真空值(包含空格或空值)

  1. StringUtils.isAnyBlank(null) = true
  2. StringUtils.isAnyBlank(null, "foo") = true
  3. StringUtils.isAnyBlank(null, null) = true
  4. StringUtils.isAnyBlank("", "bar") = true
  5. StringUtils.isAnyBlank("bob", "") = true
  6. StringUtils.isAnyBlank(" bob ", null) = true
  7. StringUtils.isAnyBlank(" ", "bar") = true
  8. StringUtils.isAnyBlank("foo", "bar") = false
  9. /**
  10. * <p>Checks if any one of the CharSequences are blank ("") or null and not whitespace only..</p>
  11. * @param css the CharSequences to check, may be null or empty
  12. * @return {@code true} if any of the CharSequences are blank or null or whitespace only
  13. * @since 3.2
  14. */
  15. public static boolean isAnyBlank(final CharSequence... css) {
  16. if (ArrayUtils.isEmpty(css)) {
  17. return true;
  18. }
  19. for (final CharSequence cs : css){
  20. if (isBlank(cs)) {
  21. return true;
  22. }
  23. }
  24. return false;
  25. }

StringUtils.isNoneBlank()

是否全部都不包含空值或空格

  1. StringUtils.isNoneBlank(null) = false
  2. StringUtils.isNoneBlank(null, "foo") = false
  3. StringUtils.isNoneBlank(null, null) = false
  4. StringUtils.isNoneBlank("", "bar") = false
  5. StringUtils.isNoneBlank("bob", "") = false
  6. StringUtils.isNoneBlank(" bob ", null) = false
  7. StringUtils.isNoneBlank(" ", "bar") = false
  8. StringUtils.isNoneBlank("foo", "bar") = true
  9. /**
  10. * <p>Checks if none of the CharSequences are blank ("") or null and whitespace only..</p>
  11. * @param css the CharSequences to check, may be null or empty
  12. * @return {@code true} if none of the CharSequences are blank or null or whitespace only
  13. * @since 3.2
  14. */
  15. public static boolean isNoneBlank(final CharSequence... css) {
  16. return !isAnyBlank(css);
  17. }

StringUtils的其他方法

可以参考官方的文档,里面有详细的描述,有些方法还是很好用的。
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html
2021-08-20-23-49-18-700000.png2021-08-20-23-49-18-875003.png