忽然想起來最近在開發過程當中,常常會遇到字符串不一樣的判空和去空格狀況,最開始老是使用==和equals來結合判空,遇到了StringUtils感受真是太方便啦!因而想經過源碼來區分一下StringUtils類經常使用的幾個方法的使用,以便記憶。css
1. StringUtils.isEmpty()html
/** * <p>Checks if a CharSequence 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 CharSequence. * That functionality is available in isBlank().</p> * * @param cs the CharSequence to check, may be null * @return {@code true} if the CharSequence is empty or null * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence) */ public static boolean isEmpty(final CharSequence cs) { return cs == null || cs.length() == 0; }
即值爲 「」 和null時,StringUtils.isEmpty()爲true;java
2. StringUtils.isNotEmpty(),與StringUtils.isEmpty()相反;非空爲true;spa
3. StringUtils.isAnyEmpty() code
/** * <p>Checks if any one of the CharSequences are empty ("") or null.</p> * * <pre> * StringUtils.isAnyEmpty(null) = true * StringUtils.isAnyEmpty(null, "foo") = true * StringUtils.isAnyEmpty("", "bar") = true * StringUtils.isAnyEmpty("bob", "") = true * StringUtils.isAnyEmpty(" bob ", null) = true * StringUtils.isAnyEmpty(" ", "bar") = false * StringUtils.isAnyEmpty("foo", "bar") = false * </pre> * * @param css the CharSequences to check, may be null or empty * @return {@code true} if any of the CharSequences are empty or null * @since 3.2 */ public static boolean isAnyEmpty(final CharSequence... css) { if (ArrayUtils.isEmpty(css)) { return true; } for (final CharSequence cs : css){ if (isEmpty(cs)) { return true; } } return false; }
因此,幾個值中任意一個爲空(「」 和null)則爲true;htm
4. StringUtils.isNoneEmpty(),幾個值都不爲空(「」 和null)則爲true;開發
5. StringUtils.isBlank()字符串
/** * <p>Checks if a CharSequence 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 cs the CharSequence to check, may be null * @return {@code true} if the CharSequence is null, empty or whitespace * @since 2.0 * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence) */ public static boolean isBlank(final CharSequence cs) { int strLen; if (cs == null || (strLen = cs.length()) == 0) { return true; } for (int i = 0; i < strLen; i++) { if (Character.isWhitespace(cs.charAt(i)) == false) { return false; } } return true; }
即值爲「 」,「」,null,StringUtils.isBlank()則爲true;不一樣於StringUtils.isEmpty(),加上判斷空白(「 」)值;源碼
6. StringUtils.isNotBlank(),和StringUtils.isBlank()相反;非空爲true;it
7. StringUtils.isAnyBlank(),任意一個爲「 」,「」,null,都爲true;
8. StringUtils.isNoneBlank(),都不爲「 」,「」,null,都爲true;
9. StringUtils.trim(),去空格
* StringUtils.trim(null) = null * StringUtils.trim("") = "" * StringUtils.trim(" ") = "" * StringUtils.trim("abc") = "abc" * StringUtils.trim(" abc ") = "abc"
你還在使用==和equals來結合判空嘛?使用StringUtils類吧,更加簡潔方便!