基于以下兩種情況。在判斷的變量是String類型時, 判斷是否為空,推薦使用isNotBlank()
.
1. isNotEmpty
不會驗證str中是否含有空字符串,而
isNotBlank
方法會驗證
public static boolean isNotEmpty(CharSequence str) {return false == isEmpty(str);
}
public static boolean isEmpty(CharSequence str) {return str == null || str.length() == 0;
}
2. isNotBlank
public static boolean isNotBlank(CharSequence str) {return false == isBlank(str);
}public static boolean isBlank(CharSequence str) {final int length;if ((str == null) || ((length = str.length()) == 0)) {return true;}for (int i = 0; i < length; i++) {// 只要有一個非空字符即為非空字符串if (false == CharUtil.isBlankChar(str.charAt(i))) {return false;}}return true;
}