org.apache.commons.lang3.StringUtils相关用法(一)----isEmpty相关

话不多说,直接上代码。小编能力有限,如有错误,烦请指正。在此提前谢过各位大佬。

1、StringUtils.isEmpty

判断是否为空。可以看到 " " 空格是会绕过这种空判断。因为是一个空格,并不是严格意义上的空值,会导致 isEmpty(" ")=false。

		/**
         * Checks if a CharSequence is empty ("") or null.
         *        StringUtils.isEmpty(null)      = true
         *        StringUtils.isEmpty("")        = true
         *        StringUtils.isEmpty(" ")       = false
         *        StringUtils.isEmpty("bob")     = false
         *        StringUtils.isEmpty("  bob  ") = false
         *
         * NOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is available in isBlank().
         * Params:
         * cs – the CharSequence to check, may be null
         * Returns:
         * 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;
	    }

2、StringUtils.isNotEmpty

与 StringUtils.isEmpty 相对,判断不为空 ,相当于 !=isEmpty()。

		/**
		 * Checks if a CharSequence is not empty ("") and not null.
		 *        StringUtils.isNotEmpty(null)      = false
		 *        StringUtils.isNotEmpty("")        = false
		 *        StringUtils.isNotEmpty(" ")       = true
		 *        StringUtils.isNotEmpty("bob")     = true
		 *        StringUtils.isNotEmpty("  bob  ") = true
		 *
		 * Params:
		 * cs – the CharSequence to check, may be null
		 * Returns:
		 * true if the CharSequence is not empty and not null
		 * Since:
		 * 3.0 Changed signature from isNotEmpty(String) to isNotEmpty(CharSequence)
		 */

	public static boolean isNotEmpty(final CharSequence cs) {
        return !isEmpty(cs);
    }

3、StringUtils.isAnyEmpty

判断是否有一个参数为空,只要有一个为空,就为 TRUE。

		/**
		 * Checks if any of the CharSequences are empty ("") or null.
		 *        StringUtils.isAnyEmpty((String) null)    = true
		 *        StringUtils.isAnyEmpty((String[]) null)  = false
		 *        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
		 *        StringUtils.isAnyEmpty(new String[]{})   = false
		 *        StringUtils.isAnyEmpty(new String[]{""}) = true
		 *
		 * Params:
		 * css – the CharSequences to check, may be null or empty
		 * Returns:
		 * 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 false;
      }
      for (final CharSequence cs : css){
        if (isEmpty(cs)) {
          return true;
        }
      }
      return false;
    }

4、StringUtils.isNoneEmpty

与 StringUtils.isAnyEmpty 相对,相当于 !isAnyEmpty(css) , 必须所有的值都不为空才返回 TRUE。

		/**
		 * Checks if none of the CharSequences are empty ("") or null.
		 *        StringUtils.isNoneEmpty((String) null)    = false
		 *        StringUtils.isNoneEmpty((String[]) null)  = true
		 *        StringUtils.isNoneEmpty(null, "foo")      = false
		 *        StringUtils.isNoneEmpty("", "bar")        = false
		 *        StringUtils.isNoneEmpty("bob", "")        = false
		 *        StringUtils.isNoneEmpty("  bob  ", null)  = false
		 *        StringUtils.isNoneEmpty(new String[] {})  = true
		 *        StringUtils.isNoneEmpty(new String[]{""}) = false
		 *        StringUtils.isNoneEmpty(" ", "bar")       = true
		 *        StringUtils.isNoneEmpty("foo", "bar")     = true
		 *
		 * Params:
		 * css – the CharSequences to check, may be null or empty
		 * Returns:
		 * true if none of the CharSequences are empty or null
		 * Since:
		 * 3.2
		 */

	public static boolean isNoneEmpty(final CharSequence... css) {
      return !isAnyEmpty(css);
    }

5、StringUtils.isAllEmpty

相当于 StringUtils.isAnyEmpty 的增强版另一版本,参数必须全部为 NULL 或 “” 才为 TRUE ,反之则为 FALSE 。

		/**
		 * Checks if all of the CharSequences are empty ("") or null.
		 *        StringUtils.isAllEmpty(null)             = true
		 *        StringUtils.isAllEmpty(null, "")         = true
		 *        StringUtils.isAllEmpty(new String[] {})  = true
		 *        StringUtils.isAllEmpty(null, "foo")      = false
		 *        StringUtils.isAllEmpty("", "bar")        = false
		 *        StringUtils.isAllEmpty("bob", "")        = false
		 *        StringUtils.isAllEmpty("  bob  ", null)  = false
		 *        StringUtils.isAllEmpty(" ", "bar")       = false
		 *        StringUtils.isAllEmpty("foo", "bar")     = false
		 *
		 * Params:
		 * css – the CharSequences to check, may be null or empty
		 * Returns:
		 * true if all of the CharSequences are empty or null
		 * Since:
		 * 3.6
		 */

	public static boolean isAllEmpty(final CharSequence... css) {
        if (ArrayUtils.isEmpty(css)) {
            return true;
        }
        for (final CharSequence cs : css) {
            if (isNotEmpty(cs)) {
                return false;
            }
        }
        return true;
    }

参考文章:
isEmpty 和 isBlank 的用法区别,居然一半的人答不上来…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值