当前位置:网站首页>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技术栈" | 否 |
这时候,如果用户输入 " " 等空白字符,这个方法也返回空了,这也是大部分业务场景下我们期望出现的结果。
边栏推荐
- Unity scene jump and exit
- Halcon knowledge: gray_ Tophat transform and bottom cap transform
- 1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
- Mixed use of fairygui button dynamics
- Meanings and differences of PV, UV, IP, VV, CV
- 1041 be unique (20 points (s)) (hash: find the first number that occurs once)
- [offer9] implement queues with two stacks
- idea中导包方法
- Database course design: college educational administration management system (including code)
- Programming homework: educational administration management system (C language)
猜你喜欢

(1) Introduction Guide to R language - the first step of data analysis

Single chip Bluetooth wireless burning

MySQL time, time zone, auto fill 0

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

Fairygui loop list

Office提示您的许可证不是正版弹框解决

idea中导包方法

There is no red exclamation mark after SVN update

Idea problem record

JS function promotion and declaration promotion of VaR variable
随机推荐
[Leetcode15]三数之和
VLSM variable length subnet mask partition tips
1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
By v$rman_ backup_ job_ Oracle "bug" caused by details
Special palindromes of daily practice of Blue Bridge Cup
[899] ordered queue
GNSS定位精度指标计算
Minio file download problem - inputstream:closed
It has been solved by personal practice: MySQL row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT
NRF24L01故障排查
Naive Bayesian theory derivation
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
(core focus of software engineering review) Chapter V detailed design exercises
Fabrication d'un sac à dos simple fairygui
[leetcode622] design circular queue
Office提示您的许可证不是正版弹框解决
@The difference between Autowired and @resource
Idea problem record
Game 280 weekly
Gateway fails to route according to the service name, and reports an error service unavailable, status=503