1.isNotBlank()方法
1 public static boolean isBlank(String str) { 2 int strLen; 3 if (str == null || (strLen = str.length()) == 0) { //判斷str是否為null或者str長度是否等於0 4 return true; 5 } 6 for (int i = 0; i < strLen; i++) { 7 if ((Character.isWhitespace(str.charAt(i)) == false)) { //空白字符的判斷 8 return false; 9 } 10 } 11 return true; 12 }
2.isNotEmpty()方法
1 public static boolean isEmpty(String str) { 2 return str == null || str.length() == 0; //判斷str的是否是null或者str長度是否等於0 3 }
可以看出isNotBlank()方法和isNotEmpty()最大的區別就是對字符串中是否有空白字符的判斷
public static void main(String[] args) { System.out.println(StringUtils.isNotEmpty(" ")); //true
System.out.println(StringUtils.isNotBlank(" ")); //false }