在Java中,我们可以依靠String.isEmpty来检查字符串的null条件吗?

72

我从一个XML文件中传入一个账户ID作为输入,稍后将对其进行解析,并在我们的代码中使用:

<accountid>123456</accountid>
<user>pavan</user>

问题在于如果不传入任何内容(即账号ID中的null值),我无法在Java代码中处理这种情况。我尝试了以下方法,但没有成功:

if (acct != null||acct==""||acct.equals("")) 
{
    // the above is not working 
}

我能够成功地处理这个问题,使用以下方法:

if(!acct.isEmpty())
{
   // thisis working 
}

我们可以依赖于String.isEmpty()方法来检查一个String的空值吗?这是有效的吗?

7个回答

166

不,绝对不能这样做——因为如果 acct 为空,它甚至都无法到达 isEmpty... 它会立即抛出一个 NullPointerException

你的测试应该是:

if (acct != null && !acct.isEmpty())
注意这里使用了&&,而不是您以前代码中的||; 还要注意,在您以前的代码中,您的条件本来就是错误的 - 即使使用&&,只有在 acct为空字符串时才会进入if体中。
或者,可以使用Guava
if (!Strings.isNullOrEmpty(acct))

1
有些人也喜欢使用这个版本的相同检查:"".equals(acct) - AlexZam
嗨Jon,我尝试了一个示例程序,String str = ""; System.out.println(str.isEmpty()); 在这里它返回true,它没有抛出NullPointerException。那么为什么我们不能使用isEmpty来检查Null值??你能否澄清一下这个问题? - user663724
@Kiran: 因为 "" 不同于空引用。试试 String str = null;,然后你会得到一个 NullPointerException。非常重要的是要理解空字符串和指向空字符串的字符串引用之间的区别。 - Jon Skeet
4
你仍然需要同时检查它是否为 null。关键是要检查字符串是否包含任何内容。 - Jon Skeet
@Kiran:你需要更具体地说明“无法处理”的情况——会发生什么? - Jon Skeet
显示剩余2条评论

35

请使用StringUtils.isEmpty代替,它也会检查null。

示例:

 StringUtils.isEmpty(null)      = true
 StringUtils.isEmpty("")        = true
 StringUtils.isEmpty(" ")       = false
 StringUtils.isEmpty("bob")     = false
 StringUtils.isEmpty("  bob  ") = false

请查看String Utils官方文档以获取更多信息。


1
仅限于链接的答案应该作为评论发布。 - NSNoob
12
但是因此我需要更多的声望。 - lradacher
StringUtils.isEmpty()现在已经被弃用了!!! ;( - VELDAS R DURAI

9
如果字符串是null,就不能使用String.isEmpty()方法。最好自己编写一个方法来检查是否为null或空字符串。
public static boolean isBlankOrNull(String str) {
    return (str == null || "".equals(str.trim()));
}

6
我建议使用commons-lang中的StringUtils而不是编写自己的方法。 - Vadim
6
我不建议将 commons-lang 添加到您的项目中(除非您已经包含它),仅仅是为了这么简单的一个方法。 - leonbloy
4
阅读以上答案,那么 str == null || str.trim().isEmpty() 怎么样? - I.G. Pascual
@ardila 这正是这个答案正在检查的内容。 - I.G. Pascual

3
不,String.isEmpty()方法的写法如下:
public boolean isEmpty() {
    return this.value.length == 0;
}

正如您所看到的,它检查字符串的长度,因此您绝对必须在之前检查字符串是否为空。


2
我认为更短的答案是:StringUtils.isBlank(acct)
根据文档: http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#isBlank%28java.lang.String%29
StringUtils.isBlank(acct)可以用来判断字符串是否为空或仅由空格字符组成。
isBlank
public static boolean isBlank(String str)
Checks if a String is whitespace, empty ("") or null.

 StringUtils.isBlank(null)      = true
 StringUtils.isBlank("")        = true
 StringUtils.isBlank(" ")       = true
 StringUtils.isBlank("bob")     = false
 StringUtils.isBlank("  bob  ") = false
 
Parameters:
str - the String to check, may be null
Returns:
true if the String is null, empty or whitespace

1
String s1=""; // empty string assigned to s1 , s1 has length 0, it holds a value of no length string

String s2=null; // absolutely nothing, it holds no value, you are not assigning any value to s2

所以 null 和空是不同的。

希望这有所帮助!!!


2
问题不在于Null和empty是否相同。OP实际上是在问他是否可以依赖于String isEmpty方法来检查null。 - NSNoob

0
您可以使用 TextUtils.isEmpty() 检查可空和空字符串。

TextUtils.isEmpty(acct)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接