【Java基础】StringUtils中isNotBlank和isNotEmpty的区别

isNotEmpty将空格也作为参数,isNotBlank则排除空格参数
1,isNotEmpty(str)等价于 str != null && str.length > 0。

2,isNotBlank(str) 等价于 str != null && str.length > 0 && str.trim().length > 0。

同理:

1,isEmpty 等价于 str == null || str.length == 0。

2,isBlank 等价于 str == null || str.length == 0 || str.trim().length == 0。

public static boolean isEmpty(String str) {
    
    
        return str == null || str.length() == 0;         //判断str的是否是null或者str长度是否等于0
     }
public static boolean isBlank(String str) {
    
    
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
    
              //判断str是否为null或者str长度是否等于0
            return true;
        }
        for (int i = 0; i < strLen; i++) {
    
    
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
    
      //空白字符的判断
                return false;
            }
        }
        return true;
    }
public static void main(String[] args) {
    
    
        System.out.println(StringUtils.isNotEmpty("  "));    //true
        System.out.println(StringUtils.isNotBlank(" "));   //false
    }

Guess you like

Origin blog.csdn.net/sunzheng176/article/details/116302765