也许你两个都不知道,也许你除了isEmpty/isNotEmpty/isNotBlank/isBlank外,并不知道还有isAnyEmpty/isNoneEmpty/isAnyBlank/isNoneBlank的存在, come on ,让我们一起来探索org.apache.commons.lang3.StringUtils;这个工具类.
StringUtils.isEmpty(null) = true StringUtils.isEmpty("") = true StringUtils.isEmpty(" ") = false StringUtils.isEmpty(“bob”) = false StringUtils.isEmpty(" bob ") = false /** * * <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) */ publicstaticbooleanisEmpty(final CharSequence cs) { returncs== null || cs.length() == 0; }
StringUtils.isNotEmpty()
相当于不为空 , = !isEmpty()
1 2 3
public static boolean isNotEmpty(final CharSequence cs) { return !isEmpty(cs); }
StringUtils.isAnyBlank(null) = true StringUtils.isAnyBlank(null, “foo”) = true StringUtils.isAnyBlank(null, null) = true StringUtils.isAnyBlank("", “bar”) = true StringUtils.isAnyBlank(“bob”, “”) = true StringUtils.isAnyBlank(" bob ", null) = true StringUtils.isAnyBlank(" ", “bar”) = true StringUtils.isAnyBlank(“foo”, “bar”) = false /** * <p>Checks if any one of the CharSequences are blank ("") or null and not whitespace only..</p> * @param css the CharSequences to check, may be null or empty * @return {@code true} if any of the CharSequences are blank or null or whitespace only * @since 3.2 */ publicstaticbooleanisAnyBlank(final CharSequence... css) { if (ArrayUtils.isEmpty(css)) { returntrue; } for (final CharSequence cs : css){ if (isBlank(cs)) { returntrue; } } returnfalse; }
StringUtils.isNoneBlank()
是否全部都不包含空值或空格
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
StringUtils.isNoneBlank(null) = false StringUtils.isNoneBlank(null, “foo”) = false StringUtils.isNoneBlank(null, null) = false StringUtils.isNoneBlank("", “bar”) = false StringUtils.isNoneBlank(“bob”, “”) = false StringUtils.isNoneBlank(" bob ", null) = false StringUtils.isNoneBlank(" ", “bar”) = false StringUtils.isNoneBlank(“foo”, “bar”) = true /** * <p>Checks if none of the CharSequences are blank ("") or null and whitespace only..</p> * @param css the CharSequences to check, may be null or empty * @return {@code true} if none of the CharSequences are blank or null or whitespace only * @since 3.2 */ publicstaticbooleanisNoneBlank(final CharSequence... css) { return !isAnyBlank(css); }