代码中 isEmpty 和 isBlank 的区别

古城微笑少年丶 2023-10-14 15:50 144阅读 0赞

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

  1. tringUtils.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 {
  14. @code true} if the CharSequence is empty or null
  15. * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
  16. */
  17. public static boolean isEmpty(final CharSequence cs) {
  18. return cs == null || cs.length() == 0;
  19. }

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 {
  11. @code true} if any of the CharSequences are empty or null
  12. * @since 3.2
  13. */
  14. public static boolean isAnyEmpty(final CharSequence... css) {
  15. if (ArrayUtils.isEmpty(css)) {
  16. return true;
  17. }
  18. for (final CharSequence cs : css){
  19. if (isEmpty(cs)) {
  20. return true;
  21. }
  22. }
  23. return false;
  24. }

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 {
  16. @code true} if none of the CharSequences are empty or null
  17. * @since 3.2
  18. */
  19. 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 {
  10. @code true} if the CharSequence is null, empty or whitespace
  11. * @since 2.0
  12. * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
  13. */
  14. public static boolean isBlank(final CharSequence cs) {
  15. int strLen;
  16. if (cs == null || (strLen = cs.length()) == 0) {
  17. return true;
  18. }
  19. for (int i = 0; i < strLen; i++) {
  20. if (Character.isWhitespace(cs.charAt(i)) == false) {
  21. return false;
  22. }
  23. }
  24. return true;
  25. }

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 {
  13. @code true} if any of the CharSequences are blank or null or whitespace only
  14. * @since 3.2
  15. */
  16. public static boolean isAnyBlank(final CharSequence... css) {
  17. if (ArrayUtils.isEmpty(css)) {
  18. return true;
  19. }
  20. for (final CharSequence cs : css){
  21. if (isBlank(cs)) {
  22. return true;
  23. }
  24. }
  25. return false;
  26. }

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 {
  13. @code true} if none of the CharSequences are blank or null or whitespace only
  14. * @since 3.2
  15. */
  16. public static boolean isNoneBlank(final CharSequence... css) {
  17. return !isAnyBlank(css);
  18. }

发表评论

表情:
评论列表 (有 0 条评论,144人围观)

还没有评论,来说两句吧...

相关阅读