当前位置:网站首页>isEmpty 和 isBlank 的用法区别
isEmpty 和 isBlank 的用法区别
2022-07-06 09:18:00 【Demon Lord(大魔王)】
首先,这两个方法用的都是工具类 StringUtils 里面的方法,都是用来判断字符串是否为空的
而这个工具类到处都是,推荐使用Apache的 不受框架的约束,方法也全
1)isEmpty
判断字符串是否为空字符串,只要有一个任意字符(包括空白字符)就不为空。
来看 isEmpty 的方法源码:
public static boolean isEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}
这个方法只判断了是为为 null 或者长度为 0。
意味着,如果用户输入 " " 等空白字符,这个方法就不通过了,结果就是不为空了。
如验证输入以下内容:
输入内容 | 是否为空 |
---|---|
" " | 否 |
"" | 是 |
"Java技术栈" | 否 |
2、isBlank
判断字符串是否为空字符串,全部空白字符也为空。
来看 isBlank 的方法源码:
public static boolean isBlank(CharSequence cs) {
int strLen = length(cs);
if (strLen == 0) {
return true;
} else {
for(int i = 0; i < strLen; ++i) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
}
}
第 7 行,只要有一个字符不为空白字符就返回 false,也就是说,如果全部都为空白字符就返回 true,也就是全部空白字符也为空。
如验证输入以下内容:
输入内容 | 是否为空 |
---|---|
" " | 是 |
"" | 是 |
"Java技术栈" | 否 |
这时候,如果用户输入 " " 等空白字符,这个方法也返回空了,这也是大部分业务场景下我们期望出现的结果。
边栏推荐
猜你喜欢
Theoretical derivation of support vector machine
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
What are the advantages of using SQL in Excel VBA
FairyGUI簡單背包的制作
idea问题记录
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
Office提示您的许可证不是正版弹框解决
Matlab读取GNSS 观测值o文件代码示例
RTKLIB: demo5 b34f.1 vs b33
随机推荐
Containers and Devops: container based Devops delivery pipeline
关于Gateway中使用@Controller的问题
Flink late data processing (3)
Fairygui character status Popup
(三)R语言的生物信息学入门——Function, data.frame, 简单DNA读取与分析
Office提示您的许可证不是正版弹框解决
数据库课程设计:高校教务管理系统(含代码)
[offer78]合并多个有序链表
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
[offer78] merge multiple ordered linked lists
Solution to the problem of automatic login in Yanshan University Campus Network
Unity场景跳转及退出
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
Vulnhub target: hacknos_ PLAYER V1.1
Mysql database index
Meanings and differences of PV, UV, IP, VV, CV
C programming exercise
JS function promotion and declaration promotion of VaR variable
Devops' future: six trends in 2022 and beyond
程序设计大作业:教务管理系统(C语言)