当前位置: 首页 > news >正文

StringUtils在commons-lang3和commons-lang中的区别,下载commons-lang3.jar包

下载地址:https://download.csdn.net/download/qq_38998213/11193418

1.isEmpty、isNotEmpty、isBlank、isNotBlank
先贴源码

    //lang
    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

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

            return true;
        } else {
            return true;
        }
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }
    //lang3
    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    public static boolean isNotEmpty(CharSequence cs) {
        return !isEmpty(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;
        }
    }

    public static boolean isNotBlank(CharSequence cs) {
        return !isBlank(cs);
    }

可以看到这几个方法逻辑毫无变化,只是参数类型变了,由String变为CharSequence。那么这个CharSequence是什么呢?我们看看它的源码:

/**
 * A <tt>CharSequence</tt> is a readable sequence of <code>char</code> values. This
 * interface provides uniform, read-only access to many different kinds of
 * <code>char</code> sequences.
 * A <code>char</code> value represents a character in the <i>Basic
 * Multilingual Plane (BMP)</i> or a surrogate. Refer to <a
 * href="Character.html#unicode">Unicode Character Representation</a> for details.
 *
 * <p> This interface does not refine the general contracts of the {@link
 * java.lang.Object#equals(java.lang.Object) equals} and {@link
 * java.lang.Object#hashCode() hashCode} methods.  The result of comparing two
 * objects that implement <tt>CharSequence</tt> is therefore, in general,
 * undefined.  Each object may be implemented by a different class, and there
 * is no guarantee that each class will be capable of testing its instances
 * for equality with those of the other.  It is therefore inappropriate to use
 * arbitrary <tt>CharSequence</tt> instances as elements in a set or as keys in
 * a map. </p>
 *
 * @author Mike McCloskey
 * @since 1.4
 * @spec JSR-51
 */

public interface CharSequence {

    int length();

    char charAt(int index);

    CharSequence subSequence(int start, int end);

    public String toString();
}
CharSequence是一个字符序列的接口,其中定义了一些常用的如length()、subSequence()等方法,String也实现了这个接口。当然大家可能在String里用到的都是subString(),实际上String也实现了subSequence()这个方法,只是直接指向了subString()。

//String中的subSequence方法
public CharSequence subSequence(int beginIndex, int endIndex) {
        return this.substring(beginIndex, endIndex);
}

lang3中使用CharSequence最大的好处就是令这些方法用处更加广泛,不止局限于String,其他一些实现了该接口的类也可以使用StringUtils中的这些方法去进行一些操作。另外我发现很多nio中的类都实现了这个接口,个人猜测可能也有为nio服务的目的。

2.equals
//lang
public static boolean equals(String str1, String str2) {
    return str1 == null?str2 == null:str1.equals(str2);
}

//lang3
public static boolean equals(CharSequence cs1, CharSequence cs2) {
        return cs1 == cs2?true:(cs1 != null && cs2 != null?(cs1.length() != cs2.length()?false:(cs1 instanceof String && cs2 instanceof String?cs1.equals(cs2):CharSequenceUtils.regionMatches(cs1, false, 0, cs2, 0, cs1.length()))):false);
}

在lang中,第一步是先判断str1是否为空,而在lang3中,第一步则是先判断两个对象是否相同。这个不难理解,如果两个对象的地址相同,那么它们指向的就是同一个对象,内容肯定相同。

3.isAnyEmpty、isNoneEmpty、isAllEmpty
    //lang3
    public static boolean isAnyEmpty(CharSequence... css) {
        if(ArrayUtils.isEmpty(css)) {
            return false;
        } else {
            CharSequence[] var1 = css;
            int var2 = css.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                CharSequence cs = var1[var3];
                if(isEmpty(cs)) {
                    return true;
                }
            }

            return false;
        }
    }

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

    public static boolean isAllEmpty(CharSequence... css) {
        if(ArrayUtils.isEmpty(css)) {
            return true;
        } else {
            CharSequence[] var1 = css;
            int var2 = css.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                CharSequence cs = var1[var3];
                if(isNotEmpty(cs)) {
                    return false;
                }
            }

            return true;
        }
    }


在lang3中,还加入了一些同时判断多个参数的方法,可以看到实际上是将参数列表放入一个CharSequence数组中,然后遍历调用之前的isEmpty等方法。判断blank也有类似的方法。

可能有人会觉得,很多方法String本身就有啊,为什么还要用StringUtils提供的呢?抛开参数类型不谈,我们可以看到,StringUtils中的方法大多都做了空校验,如果为空时会返回Null或者空串,而String本身的方法在很多传入参数或对象本身为空的时候都会报空指针错误。
 

相关文章:

  • android.os.NetworkOnMainThreadException
  • 编译release版,报错:the apk for your currently selected variant(app-release-unsigned.apk)is not signed.
  • Kotlin 条件控制
  • Debugview(VC调试工具)是一款电脑查错调试软件,该工具是程序员调试必备,能在运行程序后将错误提示完整记录在日志文本中,以供直接查看,进行故障修复,快下载使用吧!
  • Android解决读取txt文件中文乱码问题,reload和cnvert区别,按行读取txt文件,按 |进行字符串分割
  • 实用Android框架
  • git merge  gitk git入门
  • arm架构和x86架构区别
  • 怎样新建jniLibs, RTLD_LAZY) failed: dlopen failed: /data/app/com.itep.mt.dispatch-is too small to be an
  • Dart语言基础
  • Java静态内部类
  • 接口的调用怎样查找实际的调用。
  • try,catch语句,编译时候debug还会报错
  • 简单理解.net。visual studio与 .NET Framework和C#的关系
  • Visual Studio 快捷键
  • Brief introduction of how to 'Call, Apply and Bind'
  • Create React App 使用
  • export和import的用法总结
  • isset在php5.6-和php7.0+的一些差异
  • Linux编程学习笔记 | Linux IO学习[1] - 文件IO
  • MySQL用户中的%到底包不包括localhost?
  • Nacos系列:Nacos的Java SDK使用
  • Redis 中的布隆过滤器
  • Vim 折腾记
  • vue2.0开发聊天程序(四) 完整体验一次Vue开发(下)
  • windows下mongoDB的环境配置
  • 对JS继承的一点思考
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 工作手记之html2canvas使用概述
  • 开发基于以太坊智能合约的DApp
  • 马上搞懂 GeoJSON
  • 吐槽Javascript系列二:数组中的splice和slice方法
  •  一套莫尔斯电报听写、翻译系统
  • 掌握面试——弹出框的实现(一道题中包含布局/js设计模式)
  • linux 淘宝开源监控工具tsar
  • ​LeetCode解法汇总2583. 二叉树中的第 K 大层和
  • ###C语言程序设计-----C语言学习(6)#
  • #QT(一种朴素的计算器实现方法)
  • (13):Silverlight 2 数据与通信之WebRequest
  • (4) PIVOT 和 UPIVOT 的使用
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (草履虫都可以看懂的)PyQt子窗口向主窗口传递参数,主窗口接收子窗口信号、参数。
  • (附源码)springboot 房产中介系统 毕业设计 312341
  • (附源码)springboot“微印象”在线打印预约系统 毕业设计 061642
  • (附源码)springboot炼糖厂地磅全自动控制系统 毕业设计 341357
  • (篇九)MySQL常用内置函数
  • (循环依赖问题)学习spring的第九天
  • (一)Mocha源码阅读: 项目结构及命令行启动
  • (一)基于IDEA的JAVA基础10
  • (已更新)关于Visual Studio 2019安装时VS installer无法下载文件,进度条为0,显示网络有问题的解决办法
  • .net core 调用c dll_用C++生成一个简单的DLL文件VS2008
  • .NET Core使用NPOI导出复杂,美观的Excel详解
  • .NET 实现 NTFS 文件系统的硬链接 mklink /J(Junction)
  • .Net 应用中使用dot trace进行性能诊断
  • .NET 中 GetHashCode 的哈希值有多大概率会相同(哈希碰撞)