StringUtils.isBlank() vs String.isEmpty()

我遇到了一些代码,有以下:

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)之间有区别吗?

639934 次浏览

StringUtils.isBlank()也将检查null,然而:

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

如果foo为空,将抛出NullPointerException

StringUtils.isBlank(foo)将为您执行空检查。如果执行foo.isEmpty()foo为空,则会引发NullPointerException异常。

StringUtils.isBlank也返回仅为空格的true:

isBlank (String str)

检查字符串是否为空白,空("")或null。

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

警告:在.isBlank()和.isEmpty()中工作相同,只是它们不会为null返回true

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

java.lang.String.isEmpty() .

stringutil isEmpty = 字符串isEmpty检查+检查null。

stringutil isBlank = stringutil isEmpty检查+检查文本是否只包含空格字符。

有关进一步调查的有用连结:

StringUtils.isBlank()对于空格(只是空格)和null String也返回true。实际上,它修饰Char序列,然后执行检查。

当String参数中没有charsequence或String参数为null时,StringUtils.isEmpty()返回true。不同的是,如果字符串参数只包含whiltespaces, isEmpty()返回false。它认为空白是一种非空状态。

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;
}

@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

我回答这个问题是因为它是谷歌的“字符串isBlank()方法”的顶部结果。

如果你使用的是Java 11或更高版本,你可以使用字符串类isBlank()方法。这个方法的作用与Apache Commons StringUtils类相同。

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

而不是使用第三方lib,使用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

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

底线是:

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

StringUtils.isBlank(myStr)检查String myStr是否为空白,空("")或null。

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

System.out.println("".isEmpty());                      //true
System.out.println(" ".isEmpty());                     //false
System.out.println(('\u0020'+" "+'\u0020').isEmpty()); //false


System.out.println("".isBlank());                      //true
System.out.println(" ".isBlank());                     //true
System.out.println(('\u0020'+" "+'\u0020').isBlank()); //true

如果你想使用Java 8并且需要isBlank函数,那么尝试使用第三方库org.apache.commons.lang3.StringUtils

StringUtils.isBlank ()

System.out.println(StringUtils.isBlank(null));      //true
System.out.println(StringUtils.isBlank(""));        //true
System.out.println(StringUtils.isBlank(" "));       //true
System.out.println(StringUtils.isBlank("bob"));     //false
System.out.println(StringUtils.isBlank("  bob  ")); //false

StringUtils.isEmpty

System.out.println(StringUtils.isEmpty(null));      // = true
System.out.println(StringUtils.isEmpty(""));        //= true
System.out.println(StringUtils.isEmpty(" "));       // = false
System.out.println(StringUtils.isEmpty("foo"));     // = false
System.out.println(StringUtils.isEmpty("  foo  ")); //= false