StringUtils在commons-lang3和commons-lang中的区别【转】
http://blog.csdn.net/eden_m516/article/details/75042439
最近经常需要对String做一些判断和处理,于是就用到了Apache提供的StringUtils这个工具类,用的时候发现有两个不同的版本,一个版本位于org.apache.commons.lang下面,另一个则位于org.apache.commons.lang3下面。
查了一下资料,lang3是Apache Commons 团队发布的工具包,要求jdk版本在1.5以上,相对于lang来说完全支持java5的特性,废除了一些旧的API。该版本无法兼容旧有版本,于是为了避免冲突改名为lang3。这些东西就不再细说了,我们来看看StringUtils中常用的一些方法有什么改变吧。
PS.本文的java版本为1.8。
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本身的方法在很多传入参数或对象本身为空的时候都会报空指针错误。
常用方法就先介绍到这里,以后有机会再继续更。
StringUtils在commons-lang3和commons-lang中的区别【转】的更多相关文章
- struts2中的错误--java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
2013-4-7 10:13:56 org.apache.catalina.startup.HostConfig checkResources 信息: Reloading context [/chap ...
- spring异常记录-----java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
今天在练习怎样SSH中进行单元測试的时候出现下列异常: SEVERE: Exception starting filter Struts2 java.lang.NoClassDefFoundError ...
- Hadoop java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
.jar 学习好友推荐案例的时候,提交运行时报错找不到StringUtils java.lang.ClassNotFoundException: org.apache.commons.lang3.St ...
- Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
1.错误叙述性说明 2014-7-10 23:12:23 org.apache.catalina.core.StandardContext filterStart 严重: Exception star ...
- ERROR----java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
2013-4-28 13:17:57 org.apache.catalina.core.StandardContext filterStart 严重: Exception starting filte ...
- org.apache.commons.lang3.StringUtils中的StringUtils常用方法
https://my.oschina.net/funmo/blog/615202?p=1 public static void TestStr(){ //null 和 ""操作~~ ...
- org.apache.commons.lang3.tuple.Pair 作为更新参数,XML 中的 Sql 取不到值、报错
项目用的 Mybatis,今天改一个需求,落地实现是批量更新,且只需要根据主键(id)来更新一个字段(name). 于是,没有犹豫,像下面这样设计了数据结构: 既然是批量更新,那外层肯定是 List ...
- NoClassDefFoundError: org/apache/commons/lang3/StringUtils
出错信息: 2014-2-5 21:38:05 org.apache.catalina.core.StandardContext filterStart严重: Exception starting f ...
- 【java】org.apache.commons.lang3功能示例
org.apache.commons.lang3功能示例 package com.simple.test; import java.util.Date; import java.util.Iterat ...
- org.apache.commons.lang3.ArrayUtils 学习笔记
package com.nihaorz.model; /** * @作者 王睿 * @时间 2016-5-17 上午10:05:17 * */ public class Person { privat ...
随机推荐
- InnoDB缓存读命中率、使用率、脏块率(%) 缓冲池的读命中率(%) 缓冲池的利用率(%) 缓冲池脏块的百分率(%)
InnoDB缓存读命中率.使用率.脏块率(%) 缓冲池的读命中率(%) 缓冲池的利用率(%) 缓冲池脏块的百分率(%)
- pip与apt-get
在ubuntu服务器下安装包的时候,经常会用到sudo apt-get install 包名 或 sudo pip install 包名,那么两者有什么区别呢? 1.区别 pip用来安装来自PyPI( ...
- elasticsearch数据迁移——elasticsearch-dump使用
先安装好nodejs和nodejs的包管理工具npm.然后安装elasticsearch-dump: npm install elasticdump 下面迁移数据: 先在目的地址创建一个index来储 ...
- Pandas 之 过滤DateFrame中所有小于0的值并替换
Outline 前几天,数据清洗时有用到pandas去过滤大量数据中的“负值”: 把过滤出来的“负值”替换为“NaN”或者指定的值. 故做个小记录. 读取CSV文件 代码: import pandas ...
- openwrt undefined reference to ‘getpwent_r’
由于须要安装 su sudo useradd groupadd chown login 等用户管理命令,会訪问passwd文件. 用到了 getpwant_r 函数.详细是哪个命令引起的,临时不知. ...
- Leetcode 之 Valid Triangle Number
611. Valid Triangle Number 1.Problem Given an array consists of non-negative integers, your task is ...
- linux c编程:记录锁
记录锁相当于线程同步中读写锁的一种扩展类型,可以用来对有亲缘或无亲缘关系的进程进行文件读与写的同步,通过fcntl函数来执行上锁操作.尽管读写锁也可以通过在共享内存区来进行进程的同步,但是fcntl记 ...
- selenium的下拉选择框
今天总结下selenium的下拉选择框.我们通常会遇到两种下拉框,一种使用的是html的标签select,另一种是使用input标签做的假下拉框. 后者我们通常的处理方式与其他的元素类似,点击或使用J ...
- 75. Sort Colors(荷兰国旗问题 三指针)
Given an array with n objects colored red, white or blue, sort them so that objects of the same co ...
- 450. Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...