当前位置:网站首页>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技术栈" | 否 |
这时候,如果用户输入 " " 等空白字符,这个方法也返回空了,这也是大部分业务场景下我们期望出现的结果。
边栏推荐
- FairyGUI簡單背包的制作
- [leetcode19] delete the penultimate node in the linked list
- What are the functions and features of helm or terrain
- Teach you to release a DeNO module hand in hand
- NRF24L01 troubleshooting
- Database course design: college educational administration management system (including code)
- Pytorch: tensor operation (I) contiguous
- By v$rman_ backup_ job_ Oracle "bug" caused by details
- Unity3D,阿里云服务器,平台配置
- Esp8266 connect onenet (old mqtt mode)
猜你喜欢

Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports

MySQL时间、时区、自动填充0的问题

第一人称视角的角色移动

FairyGUI简单背包的制作

Gravure sans fil Bluetooth sur micro - ordinateur à puce unique

Unity3D,阿里云服务器,平台配置

Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY

FairyGUI人物状态弹窗

FairyGUI簡單背包的制作

單片機藍牙無線燒錄
随机推荐
Lock wait timeout exceeded try restarting transaction
Mixed use of fairygui button dynamics
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
Fairygui loop list
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
Flink late data processing (3)
What are the advantages of using SQL in Excel VBA
Office prompts that your license is not genuine pop-up box solution
Gateway 根据服务名路由失败,报错 Service Unavailable, status=503
Fairygui gain buff value change display
FairyGUI循環列錶
Unity场景跳转及退出
Solution to the problem of automatic login in Yanshan University Campus Network
(the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
RTKLIB: demo5 b34f.1 vs b33
Detailed explanation of truncate usage
Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
[offer18] delete the node of the linked list
GNSS定位精度指标计算