一般的判断是否为null的str 使用.isEmpty() 就可以解决或者是使用StingUtils.isEmpty(str)也可以解决。
但是由于StingUtils.isEmpty(str)和str.isEmpty()对于全由空格组成的str判断是返回true的,所以对于这种str就需要经过trim()过滤掉str两端的空格,这样就能判断str是否为空了。
public class TestEmpty {
public static void main(String[] args){
String str = " ";
if (str.trim().isEmpty()) {
System.out.println("空");
} else {
System.out.println("非空");
}
}
}
这个是我在写一个入参判断的时候想起来的