[英]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.isBlank
和java.lang.String.isEmpty
)之間有區別嗎?
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() 中的工作方式相同,除了它們不為null
返回true
。
java.lang.String.isBlank()
(從 Java 11 開始)
@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
StringUtils isEmpty = String isEmpty檢查 + 檢查是否為空。
StringUtils isBlank = StringUtils isEmpty檢查 + 檢查文本是否僅包含空白字符。
進一步調查的有用鏈接:
StringUtils.isBlank()
也會檢查是否為空,而這個:
String foo = getvalue("foo");
if (foo.isEmpty())
如果foo
為 null,將拋出NullPointerException
。
StringUtils.isBlank
也只為空格返回true
:
isBlank(字符串str)
檢查字符串是否為空格、空 ("") 或 null。
StringUtils.isBlank(foo)
將為您執行空檢查。 如果執行foo.isEmpty()
並且foo
為 null,則會引發 NullPointerException。
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
StringUtils.isBlank() 為空白(僅空格)和空字符串返回 true。 實際上它修剪 Char 序列,然后執行檢查。
StringUtils.isEmpty() 在 String 參數中沒有字符序列或 String 參數為空時返回 true。 不同之處在於,如果 String 參數僅包含空格,則 isEmpty() 返回 false。 它將空格視為非空狀態。
使用 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
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;
}
不使用第三方庫,而是使用 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 function 那么嘗試使用第三方庫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
底線是 :
isEmpty take " " as a character but isBlank not. Rest both are same.
StringUtils.isBlank(myStr) 檢查字符串 myStr 是否為空格、空("") 或 null。
我回答這個是因為它是 Google 中“String isBlank() 方法”的最高結果。
如果您使用的是 Java 11 或更高版本,則可以使用String 類的 isBlank()方法。 此方法與 Apache Commons StringUtils 類執行相同的操作。
我寫了一篇關於此方法示例的小帖子,請在此處閱讀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.