当前位置:网站首页>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技术栈" | 否 |
这时候,如果用户输入 " " 等空白字符,这个方法也返回空了,这也是大部分业务场景下我们期望出现的结果。
边栏推荐
- It has been solved by personal practice: MySQL row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT
- 编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
- JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
- MySQL performance tuning - dirty page refresh
- (4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
- FairyGUI循环列表
- FairyGUI增益BUFF數值改變的顯示
- Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
- Theoretical derivation of support vector machine
- Lean product development - Lean Software Development & lean product development
猜你喜欢

Fairygui joystick

(三)R语言的生物信息学入门——Function, data.frame, 简单DNA读取与分析

Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】

FairyGUI增益BUFF數值改變的顯示

抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现

ESP8266连接onenet(旧版MQTT方式)

Office prompts that your license is not genuine pop-up box solution

Naive Bayesian theory derivation

Latex learning

Database course design: college educational administration management system (including code)
随机推荐
FairyGUI簡單背包的制作
微信小程序开发心得
Unity3D摄像机,键盘控制前后左右上下移动,鼠标控制旋转、放缩
使用rtknavi进行RT-PPP测试
FairyGUI循环列表
音乐播放(Toggle && PlayerPrefs)
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
Force buckle 1189 Maximum number of "balloons"
【RTKLIB 2.4.3 b34 】版本更新简介一
Latex learning
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
There is no red exclamation mark after SVN update
Fairygui gain buff value change display
RTKLIB: demo5 b34f.1 vs b33
地球围绕太阳转
编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
Introduction to the daily practice column of the Blue Bridge Cup
Mixed use of fairygui button dynamics
[offer78]合并多个有序链表
Talking about the startup of Oracle Database