检查Java中的字符串是空还是空
问题描述:
我正在解析HTML数据。当要解析的单词不匹配时, String
可能是 null
或为空。
I'm parsing HTML data. The String
may be null
or empty, when the word to parse does not match.
所以,我这样写了:
if(string.equals(null) || string.equals("")){
Log.d("iftrue", "seem to be true");
}else{
Log.d("iffalse", "seem to be false");
}
当我删除 String.equals()时
,它无法正常工作。
When I delete String.equals("")
, it does not work correctly.
我认为 String.equals()
不正确。
我怎样才能最好地检查一个空的字符串
?
How can I best check for an empty String
?
答
检查null或空的正确方法如下:
Correct way to check for null or empty is like this:
if(str != null && !str.isEmpty()) { /* do your stuffs here */ }