【问题标题】:StringUtils.isBlank() vs String.isEmpty()StringUtils.isBlank() 与 String.isEmpty()
【发布时间】:2014-06-18 14:29:12
【问题描述】:

我遇到了一些具有以下内容的代码:

String foo = getvalue("foo");
if (StringUtils.isBlank(foo))
    doStuff();
else
    doOtherStuff();

这似乎在功能上等同于以下内容:

String foo = getvalue("foo");
if (foo.isEmpty())
    doStuff();
else
    doOtherStuff();

两者有区别吗(org.apache.commons.lang3.StringUtils.isBlankjava.lang.String.isEmpty)?

【问题讨论】:

  • 可能值得一提的是,还有一个 StringUtils.isEmpty(foo) 可以帮助您避免空指针,就像 isBlank 一样,但不检查空格字符。

标签: java string string-utils


【解决方案1】:

StringUtils.isBlank() 也会检查 null,而这个:

String foo = getvalue("foo");
if (foo.isEmpty())

如果foo 为空,将抛出NullPointerException

【讨论】:

  • 还有比这更大的区别;看我的回答。
  • 这是不正确的。如果 String.isEmpty() 为空,则返回 true。至少如果您谈论的是 apache.commons.lang 版本。我不确定 Spring 版本。
  • 我的评论的意思被删掉了(公平地说,它本来可以从一开始就更清楚);我没有将 StringUtils.isBlank() 与 StringUtils.isEmpty() 进行比较,而是将 StringUtils.isBlank() 与 OP 的 value.isEmpty() 进行比较
  • chut 的回答是正确的。如果 java String foo 为 null,则 foo.isEmpty() 将抛出 NullPointerException。即使 foo 为空,apache StringUtils.isBlank(foo) 也会返回 true。
【解决方案2】:

StringUtils.isBlank(foo) 将为您执行空检查。如果您执行foo.isEmpty() 并且foo 为null,您将引发NullPointerException。

【讨论】:

  • 还有比这更大的区别;看我的回答。
【解决方案3】:

StringUtils.isBlank 也返回 true 仅用于空格:

isBlank(String str)

检查字符串是否为空格、空 ("") 或 null。

【讨论】:

    【解决方案4】:

    StringUtils.isBlank() 检查字符串的每个字符是否为空白字符(或字符串是否为空或为空)。这与仅仅检查字符串是否为空是完全不同的。

    来自链接的文档:

    检查字符串是否为空格、空 ("") 或 null。

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

    比较StringUtils.isEmpty

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

    警告:在 java.lang.String.isBlank() 和 java.lang.String.isEmpty() 中的工作方式相同,只是它们不返回 @987654327 @ 代表null

    java.lang.String.isBlank()(Java 11 起)

    java.lang.String.isEmpty()

    【讨论】:

      【解决方案5】:

      StringUtils isEmpty = String isEmpty 检查 + 检查 null。

      StringUtils isBlank = StringUtils isEmpty 检查 + 检查文本是否仅包含空白字符。

      进一步调查的有用链接:

      【讨论】:

        【解决方案6】:

        StringUtils.isBlank() 为空白(仅空格)和空字符串返回 true。实际上它会修剪 Char 序列,然后执行检查。

        当 String 参数中没有字符序列或 String 参数为空时,StringUtils.isEmpty() 返回 true。不同之处在于,如果 String 参数仅包含空格,则 isEmpty() 返回 false。它将空格视为非空状态。

        【讨论】:

          【解决方案7】:
          public static boolean isEmpty(String ptext) {
           return ptext == null || ptext.trim().length() == 0;
          }
          
          public static boolean isBlank(String ptext) {
           return ptext == null || ptext.trim().length() == 0;
          }
          

          两者都有相同的代码 isBlank 如何处理空格可能你的意思是 isBlankString 这有处理空格的代码。

          public static boolean isBlankString( String pString ) {
           int strLength;
           if( pString == null || (strLength = pString.length()) == 0)
           return true;
           for(int i=0; i < strLength; i++)
           if(!Character.isWhitespace(pString.charAt(i)))
           return false;
           return false;
          }
          

          【讨论】:

            【解决方案8】:

            @arshajii 接受的答案是完全正确的。但是,下面说的更明确,

            StringUtils.isBlank()

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

            StringUtils.isEmpty

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

            【讨论】:

              【解决方案9】:

              我之所以回答这个问题,是因为它是 Google 中“String isBlank() 方法”的最高结果。

              如果您使用的是 Java 11 或更高版本,则可以使用 String class isBlank() 方法。此方法与 Apache Commons StringUtils 类做同样的事情。

              我已经写了一篇关于这个方法示例的小帖子,阅读它here

              【讨论】:

              • 投反对票,因为 Java 11 的 isBlank 不等于 StringUtils.isBlank(),因为在 Java 11 版本中缺少 null 检查。
              【解决方案10】:

              不要使用第三方库,而是使用 Java 11 isBlank()

                  String str1 = "";
                  String str2 = "   ";
                  Character ch = '\u0020';
                  String str3 =ch+" "+ch;
              
                  System.out.println(str1.isEmpty()); //true
                  System.out.println(str2.isEmpty()); //false
                  System.out.println(str3.isEmpty()); //false            
              
                  System.out.println(str1.isBlank()); //true
                  System.out.println(str2.isBlank()); //true
                  System.out.println(str3.isBlank()); //true
              

              【讨论】:

                【解决方案11】:

                isBlank() 和 isEmpty() 的唯一区别是:

                StringUtils.isBlank(" ")       = true //compared string value has space and considered as blank
                
                StringUtils.isEmpty(" ")       = false //compared string value has space and not considered as empty
                

                【讨论】:

                  【解决方案12】:

                  底线是:

                  isEmpty take " " as a character but isBlank not. Rest both are same.
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2021-11-04
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2011-11-05
                    相关资源
                    最近更新 更多