java中StringUtils中的isNotEmpty、isEmpty和isNotBlank、isBlank的区别

StringUtils中的isNotEmpty、isEmpty和isNotBlank、isBlank的区别

依赖

在这里插入图片描述

isNotEmpty和isEmpty

	isNotEmpty     判断字符串 不为空且长度不为0
	isEmpty        判断字符串 为空或长度为0

例子

StringUtils.isEmpty(null) = true //str==null
StringUtils.isEmpty("") = true //str.length()==0
StringUtils.isEmpty(" ") = false //str.length()==1
StringUtils.isEmpty("bob") = false //str.length()==3
StringUtils.isEmpty(" bob ") = false //str.length()==5

StringUtils.isNotEmpty(null) = false
StringUtils.isNotEmpty("") = false
StringUtils.isNotEmpty(" ") = true
StringUtils.isNotEmpty("bob") = true
StringUtils.isNotEmpty(" bob ") = true

底层处理

        public static boolean isNotEmpty (CharSequence cs){
            return !isEmpty(cs);
        }
        public static boolean isEmpty (CharSequence cs){
            return cs == null || cs.length() == 0;
        }

isNotBlank和isBlank

isNotBlank 	 判断字符串 不为空且长度不为0且不由空白符构成
			 (whitespace,空白符包含:空格、tab键、换行符;例如:'\n'' ''\t')
isBlank      判断字符串 为空或长度为0或由空白符(whitespace) 构成 

例子

StringUtils.isBlank(null) = true          //str==null
StringUtils.isBlank("") = true            //str.length()==0
StringUtils.isBlank(" ") = true           //空白符 空格
StringUtils.isBlank("\t \n \f \r") = true //空白符
StringUtils.isBlank("\b") = false         //"\b"为单词边界符
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false

StringUtils.isNotBlank(null) = false
StringUtils.isNotBlank("") = false
StringUtils.isNotBlank(" ") = false
StringUtils.isNotBlank(" ") = false
StringUtils.isNotBlank("\t \n \f \r") = false
StringUtils.isNotBlank("\b") = true
StringUtils.isNotBlank("bob") = true
StringUtils.isNotBlank(" bob ") = true

底层处理

        public static boolean isNotBlank (CharSequence cs){
            return !isBlank(cs);
        }
        public static boolean isBlank (CharSequence cs){
            int strLen;
            if (cs != null && (strLen = cs.length()) != 0) {
                for (int i = 0; i < strLen; ++i) {
                    if (!Character.isWhitespace(cs.charAt(i))) {
                        return false;
                    }
                }

                return true;
            } else {
                return true;
            }
        }

JAVA doc

        //isEmpty
        /**
         * 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 3.2
         */
        //isNotEmpty
        /**
         * Null-safe check if the specified collection is not empty.
         * <p>
         * Null returns false.
         *
         * @param coll  the collection to check, may be null
         * @return true if non-null and non-empty
         * @since 3.2
         */
        
        
        //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)
         */
        //isNotBlank
        /**
         * <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p>
         *
         * <pre>
         * StringUtils.isNotBlank(null)      = false
         * StringUtils.isNotBlank("")        = false
         * StringUtils.isNotBlank(" ")       = false
         * StringUtils.isNotBlank("bob")     = true
         * StringUtils.isNotBlank("  bob  ") = true
         * </pre>
         *
         * @param cs  the CharSequence to check, may be null
         * @return {@code true} if the CharSequence is
         *  not empty and not null and not whitespace
         * @since 2.0
         * @since 3.0 Changed signature from isNotBlank(String) to isNotBlank(CharSequence)
         */


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值