java判断字符串是否为空

如题所述

字符串为空有两种情况:1、"" 2 、null
  String string= "";  

//方法一 为""返回true 负责返回false 此处返回true 
  System.out.println(string.isEmpty());

//方法二 为""返回true 负责返回false 此处返回true   
  System.out.println(string.equals(""));

//方法三 为null返回true 负责返回false 此处返回false   
  System.out.println(string == null);

温馨提示:内容为网友见解,仅供参考
第1个回答  2014-04-11
/**
* @Description 判断字符串是否为空
* @date 2014年4月11日 下午3:08:56
* @param str 被判断的字符串
* @return boolean 字符串是否为空
*/
public boolean isEmptyString(String str) {
boolean isEmpty = true;

if (str != null && !"".equals(str.trim())) {
isEmpty = false;
}

return isEmpty;
}
第2个回答  推荐于2016-05-14
/**
* 判断字符串是否为空
* @param str 字符串
* @return 是否为空
*/
public static boolean isEmptyString(String str)
{
return str == null || str.trim().length() == 0;
}本回答被提问者和网友采纳
第3个回答  2014-04-11
str==null || str.trim().length == 0
相似回答