在Java中检查string是空还是空
我parsingHTML数据。 当要parsing的单词不匹配时, String可能为null或空。 
所以,我这样写:
 if(string.equals(null) || string.equals("")){ Log.d("iftrue", "seem to be true"); }else{ Log.d("iffalse", "seem to be false"); } 
 当我删除String.equals("") ,它不能正常工作。 
 我以为String.equals("")是不正确的。 
 我怎样才能最好的检查一个空的String ? 
检查null或空的正确方法是这样的:
 if(str != null && !str.isEmpty()) { /* do your stuffs here */ } 
 您可以利用Apache Commons的StringUtils.isEmpty(str) ,它检查空string,并正常处理null 。 
例:
 System.out.println(StringUtils.isEmpty("")); // true System.out.println(StringUtils.isEmpty(null)); // true 
 谷歌Guava还提供了一个类似的,可能更容易阅读的方法: Strings.isNullOrEmpty(str) 。 
例:
 System.out.println(Strings.isNullOrEmpty("")); // true System.out.println(Strings.isNullOrEmpty(null)); // true 
你可以使用Apache commons-lang
  StringUtils.isEmpty(String str) – 检查string是否为空(“”)或null。 
要么
  StringUtils.isBlank(String str) – 检查一个string是否为空白,空(“”)或null。 
后者考虑一个由空格或特殊字符组成的string,例如“”也是空的。 请参阅java.lang.Character.isWhitespace API
这样你检查string是否不为空而不是空的,也考虑到空的空间:
 boolean isEmpty = str == null || str.trim().length() == 0; if (isEmpty) { // handle the validation } 
 if(!Strings.isNullOrEmpty(String str)) { // Do your stuff here }