StringUtils
简介
org.apache.commons.lang3.StringUtils;
包含:isEmpty/isNotEmpty/isNotBlank/isBlank/isAnyEmpty/isNoneEmpty/isAnyBlank/isNoneBlank等判断方法
方法分析
isEmpty
StringUtils.isEmpty()
是否为空. 我们举例进行判断,可以看到 " " 空格是会绕过这种空判断,因为是一个空格,并不是严格的空值,会导致 isEmpty(" ")=false
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("test") = false
源码解析:
/
*
* <p>NOTE: This method changed in Lang version 2.0.
* It no longer trims the CharSequence.
* That functionality is available in isBlank().</p>
*
* @param cs the CharSequence to check, may be null
* @return {@code true} if the CharSequence is empty or null
* @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
*/
public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}
isNotEmpty
相当于不为空 , = !isEmpty()
public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
}
isAnyEmpty
是否有一个为空,只有一个为空,就为true.
举例说明:
StringUtils.isAnyEmpty(null) = true
StringUtils.isAnyEmpty(null, "test") = true
StringUtils.isAnyEmpty("", "test") = true
StringUtils.isAnyEmpty("test", "") = true
StringUtils.isAnyEmpty(" ", "test") = false
StringUtils.isAnyEmpty("test1", "test2") = false
源码解析:
/
* @param css the CharSequences to check, may be null or empty
* @return {@code true} if any of the CharSequences are empty or null
* @since 3.2
*/
public static boolean isAnyEmpty(final CharSequence... css) {
if (ArrayUtils.isEmpty(css)) {
return true;
}
for (final CharSequence cs : css){
if (isEmpty(cs)) {
return true;
}
}
return false;
}
isNoneEmpty
相当于!isAnyEmpty(css) , 必须所有的值都不为空才返回true
StringUtils.isNoneEmpty(null) = false
StringUtils.isNoneEmpty(null, "test") = false
StringUtils.isNoneEmpty("", "test") = false
StringUtils.isNoneEmpty("test", "") = false
StringUtils.isNoneEmpty("test", null) = false
StringUtils.isNoneEmpty(" ", "test") = true
StringUtils.isNoneEmpty("test1", "test2") = true
public static boolean isNoneEmpty(CharSequence... css) {
return !isAnyEmpty(css);
}
isBlank
是否为真空值(空格或者空值)
举例说明:
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("test") = false
源码解析:
public static boolean isBlank(CharSequence cs) {
int strLen;
if (cs != null && (strLen = cs.length()) != 0) {
for(int i = 0; i < strLen; ++i) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
} else {
return true;
}
}
isNotBlank()
是否真的不为空,不是空格或者空值 ,相当于!isBlank();
public static boolean isNotBlank(CharSequence cs) {
return !isBlank(cs);
}
isAnyBlank
是否包含任何真空值(包含空格或空值)
举例说明:
StringUtils.isAnyBlank(null) = true
StringUtils.isAnyBlank(null, "test") = true
StringUtils.isAnyBlank(null, null) = true
StringUtils.isAnyBlank("", "test") = true
StringUtils.isAnyBlank("test", “”) = true
StringUtils.isAnyBlank(" test ", null) = true
StringUtils.isAnyBlank(" ", "test") = true
StringUtils.isAnyBlank("test1", "test2") = false
源码解析:
public static boolean isAllBlank(CharSequence... css) {
if (ArrayUtils.isEmpty(css)) {
return true;
} else {
CharSequence[] var1 = css;
int var2 = css.length;
for(int var3 = 0; var3 < var2; ++var3) {
CharSequence cs = var1[var3];
if (isNotBlank(cs)) {
return false;
}
}
return true;
}
}
isNoneBlank
是否全部都不包含空值或空格
举例说明:
StringUtils.isNoneBlank(null) = false
StringUtils.isNoneBlank(null, "test") = false
StringUtils.isNoneBlank(null, null) = false
StringUtils.isNoneBlank("", "test") = false
StringUtils.isNoneBlank("test", "") = false
StringUtils.isNoneBlank(" test ", null) = false
StringUtils.isNoneBlank(" ", "test") = false
StringUtils.isNoneBlank("test", "test") = true
public static boolean isNoneBlank(CharSequence... css) {
return !isAnyBlank(css);
}
其他方法
StringUtils的其他方法
可以参考官方的文档,里面有详细的描述,有些方法还是很好用的.
官网