目录
StringUtils详解
1.引入依赖
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency>
2.StringUtils.isEmpty()
功能描述:判断某字符串是否为空,注意 " "(空格)不为空;
源码:
public static boolean isEmpty(final CharSequence cs) { return cs == null || cs.length() == 0; }
示例:
StringUtils.isEmpty(null) : true; StringUtils.isEmpty("") : true; StringUtils.isEmpty(" ") : false; StringUtils.isEmpty("sss") : false; StringUtils.isEmpty(" sss ") : false;
3.StringUtils.isNotEmpty()
功能描述:判断某字符串不为空,注意 " "(空格)不为空;
源码:
public static boolean isNotEmpty(final CharSequence cs) { return !isEmpty(cs); }
示例:
StringUtils.isNotEmpty(null) : false; StringUtils.isNotEmpty("") : false; StringUtils.isNotEmpty(" ") : true; StringUtils.isNotEmpty("sss") : true; StringUtils.isNotEmpty(" sss ") : true;
4.StringUtils.isAnyEmpty()
功能描述:判断输入的所有项是否有不为空的数据,,注意 " "(空格)不为空;
源码:
public static boolean isAnyEmpty(final CharSequence... css) { if (ArrayUtils.isEmpty(css)) { return false; } for (final CharSequence cs : css) { if (isEmpty(cs)) { return true; } } return false; }
示例:
StringUtils.isAnyEmpty("name",null) : true; StringUtils.isAnyEmpty("a"," ") : false; StringUtils.isAnyEmpty("") : true
5.StringUtils.isNoneEmpty()
功能描述:判断输入的所有项全部不为空,注意" "(空格)不为空
源码:
public static boolean isNoneEmpty(final CharSequence... css) { return !isAnyEmpty(css); }
示例:
StringUtils.isAnyEmpty("name",null) : false; StringUtils.isAnyEmpty("a"," ") : true; StringUtils.isAnyEmpty("") : false
6.StringUtils.isBlank()
功能描述:判断字符串是否为空,注意:单个空白符为空
源码:
public static boolean isBlank(final CharSequence cs) { final int strLen = length(cs); if (strLen == 0) { return true; } for (int i = 0; i < strLen; i++) { // 判断是否存在空白符 if (!Character.isWhitespace(cs.charAt(i))) { return false; } } return true; }
示例:
StringUtils.isBlank(null) : true; StringUtils.isBlank("") : true; StringUtils.isBlank(" ") : true; StringUtils.isBlank("ss") : false; StringUtils.isBlank(" ss ") : false;
7.StringUtils.isNotBlank()
功能描述::判断字符串是否不为空,注意:单个空白符为空
源码:
public static boolean isNotBlank(final CharSequence cs) { return !isBlank(cs); }
示例:
StringUtils.isNotBlank(null) : false; StringUtils.isNotBlank("") : false; StringUtils.isNotBlank(" ") : false; StringUtils.isNotBlank("ss") : true; StringUtils.isNotBlank(" ss ") : true;
8.StringUtils.isAnyBlank()
功能描述:判断输入的字符串项中是否存在为空的数
源码:
public static boolean isAnyBlank(final CharSequence... css) { if (ArrayUtils.isEmpty(css)) { return false; } for (final CharSequence cs : css) { if (isBlank(cs)) { return true; } } return false; }
示例:
StringUtils.isAnyBlank(null,"") : true; StringUtils.isAnyBlank(" ","sss") : true; StringUtils.isAnyBlank(""," sss") : true; StringUtils.isAnyBlank("ss","aa") : false;
9.StringUtils.trim()
功能描述:去除字符串的空白符,如果为null则返回null;
源码:
public static String trim(String str) { return str == null ? null : str.trim(); }
jdk11的utf16源码String.trim()
public static String trim(byte[] value) { int length = value.length >> 1; int len = length; int st = 0; while (st < len && getChar(value, st) <= ' ') { st++; } while (st < len && getChar(value, len - 1) <= ' ') { len--; } return ((st > 0) || (len < length )) ? new String(Arrays.copyOfRange(value, st << 1, len << 1), UTF16) : null; }
示例:
StringUtils.trim(" sss ") : "sss"; StringUtils.trim("") : ""; StringUtils.trim(" ") : ""; StringUtils.trim(null) : null;