本文讨論的 StringUtils 屬于 package org.apache.commons.lang;
文章目錄
-
-
- 字元串判空檢查
-
- "" 和 null 的差別
- isEmpty(String str)
- isBlank(String str)
- 字元串判空檢查
-
- "" 和 null 的差別
- isEmpty(String str)
- isBlank(String str)
- 集合判空檢查
-
- size==0 和 null 的差別
- isEmpty(Collection coll)
-
字元串判空檢查
要了解字元串判空方法的差別首先要了解對象為空字元串"" 和 null 的差別
“” 和 null 的差別
- null 是沒有位址的,可以了解為空指針。當對象在構造器初始化時,如果沒有被顯示的賦于初值,那麼會預設指派為 null。
- “” 空字元串是一個 String 對象是有位址的,隻是内容是空。
關于構造器初始化,在沒有顯示賦予初值的情況下。預設将數值型賦為 0 , 布爾型是 false,對象引用則是 null。
String 并不是基本資料類型,而是對象是以會被預設的賦予 null。
isEmpty() 和 isBlank() 差別在于 isBlank() 可以多了對于空格的判斷,可以根據方法名差別使用 isEmpty() 判斷字元串是否為空,而 isBlank() 則是判斷字元串是否是空格,空或null
isEmpty(String str)
isEmpty(), isNotEmpty() 判段字元串是否為空或者null,空格傳回false
/**
* <p>Checks if a String is empty ("") or null.</p>
*
* <pre>
* StringUtils.isEmpty(null) = true
* StringUtils.isEmpty("") = true
* StringUtils.isEmpty(" ") = false
* StringUtils.isEmpty("bob") = false
* StringUtils.isEmpty(" bob ") = false
* </pre>
*
* <p>NOTE: This method changed in Lang version 2.0.
* It no longer trims the String.
* That functionality is available in isBlank().</p>
*
* @param str the String to check, may be null
* @return <code>true</code> if the String is empty or null
*/
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
isBlank(String str)
isEmpty(), isNotEmpty() 判段字元串是否為空或者null,空格傳回true
/**
* <p>Checks if a String is whitespace, empty ("") or null.</p>
*
* <pre>
* StringUtils.isBlank(null) = true
* StringUtils.isBlank("") = true
* StringUtils.isBlank(" ") = true
* StringUtils.isBlank("bob") = false
* StringUtils.isBlank(" bob ") = false
* </pre>
*
* @param str the String to check, may be null
* @return <code>true</code> if the String is null, empty or whitespace
* @since 2.0
*/
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
}
本文讨論的 StringUtils 屬于 package org.apache.commons.lang;
字元串判空檢查
要了解字元串判空方法的差別首先要了解對象為空字元串"" 和 null 的差別
“” 和 null 的差別
- null 是沒有位址的,可以了解為空指針。當對象在構造器初始化時,如果沒有被顯示的賦于初值,那麼會預設指派為 null。
- “” 空字元串是一個 String 對象是有位址的,隻是内容是空。
關于構造器初始化,在沒有顯示賦予初值的情況下。預設将數值型賦為 0 , 布爾型是 false,對象引用則是 null。
String 并不是基本資料類型,而是對象是以會被預設的賦予 null。
isEmpty() 和 isBlank() 差別在于 isBlank() 可以多了對于空格的判斷,可以根據方法名差別使用 isEmpty() 判斷字元串是否為空,而 isBlank() 則是判斷字元串是否是空格,空或null
isEmpty(String str)
isEmpty(), isNotEmpty() 判段字元串是否為空或者null,空格傳回false
/**
* <p>Checks if a String is empty ("") or null.</p>
*
* <pre>
* StringUtils.isEmpty(null) = true
* StringUtils.isEmpty("") = true
* StringUtils.isEmpty(" ") = false
* StringUtils.isEmpty("bob") = false
* StringUtils.isEmpty(" bob ") = false
* </pre>
*
* <p>NOTE: This method changed in Lang version 2.0.
* It no longer trims the String.
* That functionality is available in isBlank().</p>
*
* @param str the String to check, may be null
* @return <code>true</code> if the String is empty or null
*/
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
isBlank(String str)
isEmpty(), isNotEmpty() 判段字元串是否為空或者null,空格傳回true
/**
* <p>Checks if a String is whitespace, empty ("") or null.</p>
*
* <pre>
* StringUtils.isBlank(null) = true
* StringUtils.isBlank("") = true
* StringUtils.isBlank(" ") = true
* StringUtils.isBlank("bob") = false
* StringUtils.isBlank(" bob ") = false
* </pre>
*
* @param str the String to check, may be null
* @return <code>true</code> if the String is null, empty or whitespace
* @since 2.0
*/
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
}
本文讨論的 CollectionUtils 屬于 package org.apache.commons.collections;
集合判空檢查
要了解集合判空方法的差別首先要了解對象為size == 0 和 null 的差別
size==0 和 null 的差別
- null 是沒有位址的,可以了解為空指針。當對象在構造器初始化時,如果沒有被顯示的賦于初值,那麼會預設指派為 null。
- size==0 表示集合已經指向一個位址,但是指向的對象中沒有元素。
isEmpty() 和 isBlank() 差別在于 isBlank()
isEmpty(Collection coll)
isEmpty(), isNotEmpty() 判段集合是否為null或者不包含任何元素,null 傳回true
/**
* Null-safe check if the specified collection is empty.
* <p>
* Null returns true.
*
* @param coll the collection to check, may be null
* @return true if empty or null
* @since Commons Collections 3.2
*/
public static boolean isEmpty(Collection coll) {
return (coll == null || coll.isEmpty());
}
/**
* Returns <tt>true</tt> if this collection contains no elements.
*
* @return <tt>true</tt> if this collection contains no elements
*/
boolean isEmpty();