判断字符串是否为空,是否只有空格
来源:互联网 发布:淘宝联盟5.2苹果版本 编辑:程序博客网 时间:2024/06/05 03:17
方式一:自己判断
str != null && str.length() != 0
alternatively
str != null && !str.equals("")
or
str != null && !"".equals(str)
Note: The second check (first and second alternatives) assumes str is not null. It's ok only because the first check is doing that (and Java doesn't does the second check if the first is false)!
IMPORTANT: DON'T use == for string equality. == checks the pointer is equal, not the value. Two strings can be in different memory addresses (two instances) but have the same value!
2 引入外部类
Almost every library I know defines a utility class called StringUtils
, StringUtil
or StringHelper
, and they usually include the method you are looking for.
My personal favorite is Apache Commons / Lang, where in the StringUtils class, you get both the
- StringUtils.isEmpty(String) and the
- StringUtils.isBlank(String) method
(The first checks whether a string is null or empty, the second checks whether it is null, empty or whitespace only)
Just adding Android in here:
import android.text.TextUtils;if (!TextUtils.isEmpty(str)) {...}
- 判断字符串是否为空,是否只有空格
- JAVA判断字符串是否为空,是否只有空格
- java 判断字符串是否为空,空字符串,或者空格
- js判断输入字符串是否为空、空格、null总结
- js判断输入字符串是否为空、空格、null总结
- js判断输入字符串是否为空、空格、null总结
- 判断字符串是否为空格
- 判断字符串是否为空
- 判断是否为空字符串
- 判断字符串是否为空
- 判断字符串是否为空
- 判断字符串是否为空
- 判断字符串 是否为空
- 判断字符串是否为空
- 判断字符串是否为空
- 判断字符串是否为空
- 判断字符串是否为空
- 判断字符串是否为空
- Sublime Text 2 - 性感无比的代码编辑器!程序员必备神器!跨平台支持Win/Mac/Linux
- linux下程序的前、后台切换及让进程不受终端的影响
- Android权限之sharedUserId和签名
- Android Service 服务(一)—— Service
- 精简代码,为网站减负的十大建议
- 判断字符串是否为空,是否只有空格
- 计算机应用是研究计算机应用于各个领域的理论
- 4月15日 训练
- sql server 异地备份与删除
- view的几种布局方式
- Android AIDL
- hdu 3829 Cat VS Dog 最大点独立集
- C语言scanf函数
- android开发--动态设置键盘弹出与view的交互样式