看下官网描述:
The standard Java libraries fail to provide enough methods for manipulation of its core classes. Apache Commons Lang provides these extra methods.
Lang provides a host of helper utilities for the java.lang API, notably String manipulation methods, basic numerical methods, object reflection, concurrency, creation and serialization and System properties. Additionally it contains basic enhancements to java.util.Date and a series of utilities dedicated to help with building methods, such as hashCode, toString and equals.
Note that Lang 3.0 (and subsequent versions) use a different package (org.apache.commons.lang3) than the previous versions (org.apache.commons.lang), allowing it to be used at the same time as an earlier version
主要说明,针对标准java库方法的匮乏,apache提供了一些扩展的类库供大家使用,这些类库包含了很多方面,比如用于操作字符串的,数字的,对象反射的,并发的,系统的等等
并且3.0版本以上提供了一个不同的版本org.apache.commons.lang,还可以使用旧版本org.apache.commons.lang
maven版本:
<!-- common工具类包 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
主要的包:
org.apache.commons.lang3:Provides highly reusable static utility methods,主要是一些可以高度重用的Util类
org.apache.commons.lang3.arch:Provides classes to work with the values of the os.arch system property.
org.apache.commons.lang3.builder:Assists in creating consistent equals(Object), toString(), hashCode(), and compareTo(Object) methods.
org.apache.commons.lang3.concurrent:Provides support classes for multi-threaded programming.
org.apache.commons.lang3.event:Provides some useful event-based utilities.
org.apache.commons.lang3.exception:Provides functionality for Exceptions.
org.apache.commons.lang3.math:Extends java.math for business mathematical classes.
org.apache.commons.lang3.mutable:Provides typed mutable wrappers to primitive values and Object.
org.apache.commons.lang3.reflect:Accumulates common high-level uses of the java.lang.reflect APIs.
org.apache.commons.lang3.text:Provides classes for handling and manipulating text, partly as an extension to java.text.
org.apache.commons.lang3.text.translate :An API for creating text translation routines from a set of smaller building blocks.
org.apache.commons.lang3.time:Provides classes and methods to work with dates and durations.
org.apache.commons.lang3.tuple:Tuple classes, starting with a Pair class in version 3.0.
下面简单描述下commons.lang3包下的静态util类org.apache.commons.lang3.StringUtils
StringUtils是JDK提供的String类型操作方法的补充,并且是null安全的(即如果输入参数String为null则不会抛出NullPointerException
另外注意,
此静态类操作对象的类型大都由String变为CharSequence,CharSequence是一个字符序列的接口,其中定义了一些常用的如length()、subSequence()等方法,String也实现了这个接口
lang3中使用CharSequence最大的好处就是令这些方法用处更加广泛,不止局限于String,其他一些实现了该接口的类也可以使用StringUtils中的这些方法去进行一些操作
一个使用举例:
public class Demo {
public static void main(String[] args) {
String [] arr_str = {"code4","code2","code3"};
List<String> list1 = Arrays.asList(arr_str);
System.out.println(StringUtils.isEmpty(null)); // true
System.out.println(StringUtils.isEmpty(" ")); // false
System.out.println(StringUtils.isBlank(" ")); // true
System.out.println(StringUtils.reverse("china").toString());
String [] arr1 = StringUtils.split("code1,code2,code3",",");
for (String item : arr1) {
System.out.println(item);
}
}
}