方法过期的替代方法

时间:2025-04-06 07:51:49

1、问题概述?

今天在准备使用StringUtils.isEmpty()工具判断字符串是否为空的时候,突然发现idEmpty上出现了横线,这就表示这个方法可以实现但是已经过期了,官方不推荐使用了。

原因其实是因为有人给官方提交了一个问题,()可能会导致一个隐藏bug。官方直接弃用,推荐使用hasTest或者hasLength。

两者用法基本相同,含义基本相同只说明其中一个。

2、解决办法

这个时候我们可以使用(token)替代之前的StringUtils.isEmpty()工具类型。也非常的好用。

这是由spring框架提供的一个工具方法

功能:如果字符不为 null 值,且字符的长度大于0,且不是空白字符,则返回 true,否则返回false.

具体用法如下:

public static void main(String[] args) {
    (("123 "));//true
    (("123"));//ture
    ((""));//false
    ((" "));//false
    ((null));//false
}

3、源码观看

 public static boolean hasText(@Nullable CharSequence str) {
        if (str == null) {
            return false;
        } else {
            int strLen = ();
            if (strLen == 0) {
                return false;
            } else {
                for(int i = 0; i < strLen; ++i) {
                    if (!((i))) {
                        return true;
                    }
                }

                return false;
            }
        }
    }

    public static boolean hasText(@Nullable String str) {
        return str != null && !();
    }