Java StringUtils.isBlank() 与 String.isEmpty()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23419087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
StringUtils.isBlank() vs String.isEmpty()
提问by NSA
I ran into some code that has the following:
我遇到了一些具有以下内容的代码:
String foo = getvalue("foo");
if (StringUtils.isBlank(foo))
doStuff();
else
doOtherStuff();
This appears to be functionally equivalent to the following:
这似乎在功能上等同于以下内容:
String foo = getvalue("foo");
if (foo.isEmpty())
doStuff();
else
doOtherStuff();
Is a difference between the two (org.apache.commons.lang3.StringUtils.isBlank
and java.lang.String.isEmpty
)?
两者(org.apache.commons.lang3.StringUtils.isBlank
和java.lang.String.isEmpty
)之间有区别吗?
采纳答案by arshajii
StringUtils.isBlank()
checks that each character of the string is a whitespace character (or that the string is empty or that it's null). This is totally different than just checking if the string is empty.
StringUtils.isBlank()
检查字符串的每个字符是否为空白字符(或者字符串是否为空或为空)。这与仅检查字符串是否为空完全不同。
From the linked documentation:
从链接的文档:
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
检查字符串是否为空格、空 ("") 或 null。
StringUtils.isBlank(null) ? ? ?= true StringUtils.isBlank("") ? ? ? ?= true StringUtils.isBlank(" ") ? ? ? = true StringUtils.isBlank("bob") ? ? = false StringUtils.isBlank(" ?bob ?") = false
For comparison StringUtils.isEmpty:
为了比较StringUtils.isEmpty:
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
Warning: In java.lang.String.isBlank() and java.lang.String.isEmpty() work the same except they don't return true
for null
.
警告:在java.lang.String.isBlank() 和java.lang.String.isEmpty() 中的工作方式相同,只是它们不返回true
for null
。
回答by chut
StringUtils.isBlank()
will also check for null, whereas this:
StringUtils.isBlank()
还将检查空值,而这:
String foo = getvalue("foo");
if (foo.isEmpty())
will throw a NullPointerException
if foo
is null.
将抛出一个NullPointerException
iffoo
为空。
回答by Default
StringUtils.isBlank(foo)
will perform a null check for you. If you perform foo.isEmpty()
and foo
is null, you will raise a NullPointerException.
StringUtils.isBlank(foo)
将为您执行空检查。如果执行foo.isEmpty()
并且foo
为空,则会引发 NullPointerException。
回答by Octavia Togami
StringUtils.isBlank
also returns true
for just whitespace:
StringUtils.isBlank
也只返回true
空格:
isBlank(String str)
Checks if a String is whitespace, empty ("") or null.
isBlank(字符串str)
检查字符串是否为空格、空 ("") 或 null。
回答by yallam
StringUtils isEmpty= String isEmptychecks + checks for null.
StringUtils isEmpty= String isEmpty检查 + 检查是否为空。
StringUtils isBlank= StringUtils isEmptychecks + checks if the text contains only whitespace character(s).
StringUtils isBlank= StringUtils isEmpty检查 + 检查文本是否仅包含空白字符。
Useful links for further investigation:
进一步调查的有用链接:
回答by user4555388
StringUtils.isBlank() returns true for blanks(just whitespaces)and for null String as well. Actually it trims the Char sequences and then performs check.
StringUtils.isBlank() 为空白(仅空格)和空字符串返回 true。实际上它修剪 Char 序列,然后执行检查。
StringUtils.isEmpty() returns true when there is no charsequence in the String parameter or when String parameter is null. Difference is that isEmpty() returns false if String parameter contains just whiltespaces. It considers whitespaces as a state of being non empty.
StringUtils.isEmpty() 在 String 参数中没有字符序列或 String 参数为空时返回 true。不同之处在于,如果 String 参数仅包含空格,则 isEmpty() 返回 false。它将空格视为非空状态。
回答by Apoorva Dhanvanthri
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;
}
Both have the same code how will isBlank handle white spaces probably you meant isBlankString this has the code for handling whitespaces.
两者都有相同的代码 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;
}
回答by nilesh
The accepted answer from @arshajii is totally correct. However just being more explicit by saying below,
@arshajii 接受的答案是完全正确的。然而,只是在下面说得更明确,
StringUtils.isBlank()
StringUtils.isBlank()
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
StringUtils.isEmpty
StringUtils.isEmpty
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
回答by Pankaj
I am answering this because it's the top result in Google for "String isBlank() Method".
我回答这个是因为它是 Google 中“String isBlank() 方法”的最高结果。
If you are using Java 11 or above, you can use the String class isBlank()method. This method does the same thing as Apache Commons StringUtils class.
如果您使用的是 Java 11 或更高版本,则可以使用String 类的 isBlank()方法。此方法与 Apache Commons StringUtils 类执行相同的操作。
I have written a small post on this method examples, read it here.
我写了一篇关于此方法示例的小帖子,请在此处阅读。
回答by ravthiru
Instead of using third party lib, use Java 11 isBlank()
使用 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