java if判断字符串非空


1 public static void main(String[] args) {
 2         String s = null;
 3         String c = "        ";
 4         String a = new String("");
 5         String b = new String("");
 6         System.out.println("s:" + s);
 7         System.out.println("a:" + a);
 8         System.out.println("b:" + b);
 9         //根据内存地址判断 false
10         if (a == b){
11             System.out.println("1");
12         }
13         //false
14         if (StringUtils.isNoneEmpty(s)){
15             System.out.println("2");
16         }
17         //false
18         if (!"".equals(a)){
19             System.out.println("3");
20         }
21         //false
22         if (isNull(c)){
23             System.out.println("4");
24         }
25         //c:true  s:false a:false
26         //检查是否为("") or null 非空进入
27         if (StringUtils.isNoneEmpty(c)){
28             System.out.println("5");
29         }
30         //true null.trim()报错
31         //trim()去除前后的空格
32         if ("".equals(a.trim()) ){
33             System.out.println("6");
34         }
35         //false 空串不为null
36         if (c.trim().equals(null)){
37             System.out.println("7");
38         }
39         //s:true c:false a:true
40         if (isEmpty(s)) {
41             System.out.println("8");
42         }
43     }

一次做判断时的小总结。简单的测试了这些字符串判空的方法,如果有错误或补充请道友留言补充!


原文链接:https://www.cnblogs.com/Tie-shu/p/13518553.html