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

Posted

技术标签:

【中文标题】我们可以依靠 String.isEmpty 来检查 Java 中字符串的空条件吗?【英文标题】:Can we rely on String.isEmpty for checking null condition on a String in Java? 【发布时间】:2012-01-23 10:11:09 【问题描述】:

我正在传递一个 accountid 作为 XML 文件的输入,如图所示,稍后将对其进行解析并在我们的代码中使用:

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

问题是,如果没有传递任何内容(acoutnid 中的空值)作为 accountid 传递,我无法在 Java 代码中处理这种情况。我试过了,但没有成功:

if (acct != null||acct==""||acct.equals("")) 

    // the above is not working 

我能够使用以下方法成功处理此问题:

if(!acct.isEmpty())

   // thisis working 

我们可以依靠String.isEmpty() 方法来检查String 的空条件吗?这有效吗?

【问题讨论】:

【参考方案1】:

不,绝对不是 - 因为如果 acct 为空,它甚至不会 getisEmpty... 它会立即抛出 NullPointerException

你的测试应该是:

if (acct != null && !acct.isEmpty())

注意这里使用&amp;&amp;,而不是前面代码中的||;另请注意,在您之前的代码中,您的条件无论如何都是错误的 - 即使使用&amp;&amp;,如果acct 是一个空字符串,您也只会输入if 正文。

或者,使用Guava:

if (!Strings.isNullOrEmpty(acct))

【讨论】:

有些人也喜欢这个版本的同款检查:"".equals(acct) 嗨乔恩,我尝试了一个示例程序,字符串 str = ""; System.out.println(str.isEmpty()); ,这里它返回 true ,它没有抛出 NullPointerException 。那么为什么我们不能使用 isEmpty 来检查 Null 值?你能澄清一下吗?? @Kiran:因为“”与空引用不同。试试String str = null;然后你会得到一个NullPointerException。理解为空的字符串引用和引用空字符串的字符串引用之间的区别非常重要。 @AlexZam:您仍然想检查它是否为空。重点是检查字符串是否有内容。 谢谢乔恩,我尝试了你告诉的这段代码,如果 (acct != null && !acct.isEmpty()) ,但是当没有 accountid 传递时它仍然无法处理。 【参考方案2】:

改用StringUtils.isEmpty,它也会检查空值。

例如:

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

在official Documentation on String Utils上查看更多信息。

【讨论】:

只有链接的答案应该以 cmets 的形式发布 但因此我需要更多的声誉【参考方案3】:

如果String.isEmpty() 为空,则不能使用它。最好是有自己的方法来检查 null 或空。

public static boolean isBlankOrNull(String str) 
    return (str == null || "".equals(str.trim()));

【讨论】:

我建议使用 commons-lang 中的 StringUtils 而不是自己编写。 +1 我会建议将 commons-lang 添加到您的项目中(除非您已经包含它)仅用于这种简单的方法。 看了以上答案,str == null || str.trim().isEmpty()怎么样? @ardila 这正是这个答案正在检查的内容【参考方案4】:

不,String.isEmpty() 方法如下所示:

public boolean isEmpty() 
    return this.value.length == 0;

如您所见,它会检查字符串的长度 所以你必须先检查字符串是否为空。

【讨论】:

【参考方案5】:

我认为您会喜欢的更简短的答案是: 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

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

【讨论】:

【参考方案6】:
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 不等于空。

希望对你有帮助!!!

【讨论】:

问题不在于 Null 和 empty 是否相同。相反,OP 询问他是否可以依靠 String isEmpty 方法来检查 null。

以上是关于我们可以依靠 String.isEmpty 来检查 Java 中字符串的空条件吗?的主要内容,如果未能解决你的问题,请参考以下文章

从源码分析java.lang.String.isEmpty()

缺少 String.IsEmpty 方法

依靠 /proc/[pid]/status 检查另一个进程的身份

我可以依靠 malloc 返回 NULL 吗?

java String isEmpty Utility

java.lang.NoSuchMethodError: java.lang.String.isEmpty()Z