检查字符串是否为空或非空如何检查字符串是否为空或非空?public void doStuff(String str){
if (str != null && str != "**here I want to check the 'str' is empty or not**")
{
/* handle empty string */
}
/* ... */}
3 回答
小唯快跑啊
TA贡献1863条经验 获得超2个赞
if(str != null && !str.isEmpty())
&&&&str.isEmpty()str
str.length() == 0
if(str != null && !str.trim().isEmpty())
str.trim().isEmpty()str.isBlank()
public static boolean empty( final String s ) {
// Null-safe, short-circuit evaluation.
return s == null || s.trim().isEmpty();}if( !empty( str ) )
慕沐林林
TA贡献2016条经验 获得超9个赞
使用 org.apache.Commons.lang.StringUtils
import org.apache.commons.lang.StringUtils;if (StringUtils.isNotBlank(str)) {
...} if (StringUtils.isBlank(str)) {
...}添加回答
举报
0/150
提交
取消
