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 //根據記憶體地址判斷 false10 if (a == b){11 System.out.println("1");12 }13 //false14 if (StringUtils.isNoneEmpty(s)){15 System.out.println("2");16 }17 //false18 if (!"".equals(a)){19 System.out.println("3");20 }21 //false22 if (isNull(c)){23 System.out.println("4");24 }25 //c:true s:false a:false26 //檢查是否為("") 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 空串不為null36 if (c.trim().equals(null)){37 System.out.println("7");38 }39 //s:true c:false a:true40 if (isEmpty(s)) {41 System.out.println("8");42 }43 }
一次做判斷時的小總結,簡單的測驗了這些字串判空的方法,如果有錯誤或補充請道友留言補充!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/71626.html
標籤:Java
