StringUtils 是 package org.apache.commons.lang 包下面的工具类。
//具体的判断,以供参考
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
//源码
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
StringUtils.isNotEmpty(null) = false
StringUtils.isNotEmpty("") = false
StringUtils.isNotEmpty(" ") = true
StringUtils.isNotEmpty("bob") = true
StringUtils.isNotEmpty(" bob ") = true
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
//源码
//加了对空格的验证
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
}
StringUtils.isNotBlank(null) = false
StringUtils.isNotBlank("") = false
StringUtils.isNotBlank(" ") = false
StringUtils.isNotBlank("bob") = true
StringUtils.isNotBlank(" bob ") = true
StringUtils.clean(null) = ""
StringUtils.clean("") = ""
StringUtils.clean("abc") = "abc"
StringUtils.clean(" abc ") = "abc"
StringUtils.clean(" ") = ""
// 源码
//定义的EMPTY 常量
public static final String EMPTY = "";
public static String clean(String str) {
return str == null ? EMPTY : str.trim();
}
StringUtils.trim(null) = null
StringUtils.trim("") = ""
StringUtils.trim(" ") = ""
StringUtils.trim("abc") = "abc"
StringUtils.trim(" abc ") = "abc"
//源码部分
public static String trim(String str) {
return str == null ? null : str.trim();
}
StringUtils.strip(null) = null
StringUtils.strip("") = ""
StringUtils.strip(" ") = ""
StringUtils.strip("abc") = "abc"
StringUtils.strip(" abc") = "abc"
StringUtils.strip("abc ") = "abc"
StringUtils.strip(" abc ") = "abc"
StringUtils.strip(" ab c ") = "ab c"
//源码
public static String strip(String str) {
return strip(str, null);
}
StringUtils.join(null) = null
StringUtils.join([]) = ""
StringUtils.join([null]) = ""
StringUtils.join(["a", "b", "c"]) = "abc"
StringUtils.join([null, "", "a"]) = "a"
// 源码部分
public static String join(Object[] array) {
return join(array, null);
}
//指定拼接符
public static String join(Object[] array, String separator) {
if (array == null) {
return null;
}
return join(array, separator, 0, array.length);
}
//指定拼接的起始的索引
public static String join(Object[] array, String separator, int startIndex, int endIndex) {
if (array == null) {
return null;
}
if (separator == null) {
separator = EMPTY;
}