org.apache.commons.lang3.StringUtils (掌握)
Maven依赖
org.apache.commons
commons-lang3
3.9
示例
import org.apache.commons.lang3.StringUtils;
public class DemoTest {
public static void main(String[] args) {
String str = null;
System.out.println(StringUtils.isBlank(str)); //true
System.out.println(StringUtils.isEmpty(str)); //true
str = "";
System.out.println(StringUtils.isBlank(str)); //true
System.out.println(StringUtils.isEmpty(str)); //true
str = " ";
System.out.println(StringUtils.isBlank(str)); //true
System.out.println(StringUtils.isEmpty(str)); //false
}
}
记忆:isBlank()在参数为null、""、" "时,值全部为true
,在实际项目中使用到该方法比较多。
-
英语 empty 英 [ˈempti] 美 [ˈempti] adj. 空的;空洞的;空虚的; blank 英 [blæŋk] 美 [blæŋk] adj. 空白的;空的;
-
isEmpty:判断字符串是否为空字符串,只要有任意一个字符(包括空白字符)就不为空。 其源代码:
public static boolean isEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}
-
isBlank:判断字符串是否为空字符串,全部空白字符也为空。
-
源代码
public static boolean isBlank(CharSequence cs) {
int strLen;
if (cs != null && (strLen = cs.length()) != 0) {
for(int i = 0; i
关注
打赏