今天在编码的过程中,对于null,采用==null进行判断。并且为了过滤"",使用了str.trim().length()==0,当str为null时,报空指针异常。

于是决定使用Apache的commons-lang,简化代码的同时也减少程序的bug。现对学习内容进行总结。

  首先导入commons-lang包,为方便测试,导入Junit进行单元测试。代码如下:

  

public class Main {
Logger logger = Logger.getLogger(Main.class.getName()); public static void main(String[] args) {
System.out.println("Hello World!");
} @Test
public void equalsTest() {
System.out.println(StringUtils.equals("he", "he")); //T
System.out.println(StringUtils.equals("he", "ho")); //F
System.out.println(StringUtils.equals("he", "HE")); //F System.out.println(StringUtils.equalsIgnoreCase("he", "he")); //T
System.out.println(StringUtils.equalsIgnoreCase("he", "ho")); //F
System.out.println(StringUtils.equalsIgnoreCase("he", "HE")); //T } @Test
public void testIsEmpty(){
System.out.println(StringUtils.isEmpty(null)); //T
System.out.println(StringUtils.isEmpty("")); //T
System.out.println(StringUtils.isEmpty(" ")); //F
System.out.println(StringUtils.isEmpty("bob")); //F
System.out.println(StringUtils.isEmpty(" bob ")); //F
} @Test
public void testIsNotEmpty(){
System.out.println(StringUtils.isNotEmpty(null)); //F
System.out.println(StringUtils.isNotEmpty("")); //F
System.out.println(StringUtils.isNotEmpty(" ")); //T
System.out.println(StringUtils.isNotEmpty("bob")); //T
System.out.println(StringUtils.isNotEmpty(" bob ")); //T
} @Test
public void testIsBlank(){
System.out.println(StringUtils.isBlank(null)); // T
System.out.println(StringUtils.isBlank("")); // T
System.out.println(StringUtils.isBlank(" ")); // T
System.out.println(StringUtils.isBlank("bob")); //F
System.out.println(StringUtils.isBlank(" bob ")); //F
} @Test
public void testIsNotBlank(){
System.out.println(StringUtils.isNotBlank(null)); //F
System.out.println(StringUtils.isNotBlank("")); //F
System.out.println(StringUtils.isNotBlank(" ")); //F
System.out.println(StringUtils.isNotBlank("bob")); // T
System.out.println(StringUtils.isNotBlank(" bob ")); // T
} //public static String[] split(String str,String separatorChars)
@Test
public void testSplit() {
//默认半角空格分割
String str1 = "aaa bbb ccc";
String[] dim1 = StringUtils.split(str1); // => ["aaa", "bbb", "ccc"]
for(int i = 0;i<dim1.length;i++) {
System.out.println(dim1[i]); //
} String contrivedExampleString = "one.two.three.four";
String[] result = contrivedExampleString.split(".");
System.out.println(result.length); // 0 //指定分隔符
String[] res= StringUtils.split(contrivedExampleString,".");
for(int i = 0;i<res.length;i++) {
System.out.println(res[i]); //
} //去除空字符串
String str3 = "aaa,,bbb";
String[] dim3 = StringUtils.split(str3, ","); // => ["aaa", "bbb"]
for(int i = 0;i<dim3.length;i++) {
System.out.println(dim3[i]); //
} //包含空字符串
String str4 = "aaa,,bbb";
String[] dim4 = StringUtils.splitPreserveAllTokens(str4, ","); // => ["aaa", "", "bbb"]
System.out.println(dim4.length);//
} @Test
public void testJoin() {
String[] numbers = {"one", "two", "three"};
String numberStr= StringUtils.join(numbers,",");
System.out.println(numberStr); // returns "one,two,three"
} @Test
public void trimTest() {
System.out.println(StringUtils.trim(null)); // null System.out.println(StringUtils.trim("")); // "" System.out.println(StringUtils.trim(" ")); // "" System.out.println(StringUtils.trim("abc")); // "abc" System.out.println(StringUtils.trim(" abc")); // "abc" System.out.println(StringUtils.trim(" abc ")); // "abc" System.out.println(StringUtils.trim(" ab c ")); // "ab c"
} @Test
public void stripTest() {
System.out.println(StringUtils.strip(null)); // null System.out.println(StringUtils.strip("")); // "" System.out.println(StringUtils.strip(" ")); // "" System.out.println(StringUtils.strip("abc")); // "abc" System.out.println(StringUtils.strip(" abc")); // "abc" System.out.println(StringUtils.strip("abc ")); // "abc" System.out.println(StringUtils.strip(" abc ")); // "abc" System.out.println(StringUtils.strip(" ab c ")); // "ab c"
} @Test
public void testCountMatches() {
int nCount = StringUtils.countMatches("UPDATE tb_table SET xx=?,xyz=?, sss=? WHERE id=?", "?");
System.out.println(nCount); //
} @Test
public void reverseTest() {
String str = "hello";
String res= StringUtils.reverse(str); //olleh
System.out.println(res);
} @Test
public void repeatTest() {
String str = "hello";
String res1= StringUtils.repeat(str, 3); //hellohellohello
String res2= StringUtils.repeat(str,",",3); //hello,hello,hello
System.out.println(res1);
System.out.println(res2);
}
}

  参考文献

  commons-lang3-3.4-src\src\test\

  http://www.cnblogs.com/ITtangtang/p/3966955.html

  http://ray-yui.iteye.com/blog/1958319

commons-lang的更多相关文章

  1. Apache Commons Lang

    http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/package- ...

  2. 让时间处理简单化 【第三方扩展类库org.apache.commons.lang.time】

    JAVA的时间日期处理一直是一个比较复杂的问题,大多数程序员都不能很轻松的来处理这些问题.首先Java中关于时间的类,从 JDK 1.1 开始,Date的作用很有限,相应的功能已由Calendar与D ...

  3. 关于出现 org.apache.commons.lang.exception.NestableRuntimeException的解决方法

    最近做服务端和客户端之间的访问,出现了 org.apache.commons.lang.exception.NestableRuntimeException等状况.实在令人头大,翻到了一个很好的帖子说 ...

  4. org.apache.commons.lang.StringUtils中常用的方法

    org.apache.commons.lang.StringUtils中常用的方法,这里主要列举String中没有,且比较有用的方法: 1. 检查字符串是否为空: static boolean isB ...

  5. java转换json需要导入的jar包,org/apache/commons/lang/exception/NestableRuntimeException

    缺少相应jar包都会有异常,根据异常找jar包导入......     这里我说下lang包,因为这个包我找了好半天:   我用的是: commons-lang3-3.1.jar  出现异常: jav ...

  6. org.apache.commons.lang.StringUtils类

    org.apache.commons.lang.StringUtils类 本文摘自:(http://www.blogjava.net/japper/archive/2012/05/23/378946. ...

  7. java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntim [问题点数:40分,结帖人wangxiaohua_001]

    14:56:10.093 WARN!! Error for /butterfly/plugins/zhonghang/UsefulData/save_usefuldata.bshjava.lang.N ...

  8. java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils

    java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils Caused by: java.lang.ClassNotFou ...

  9. ssh整合启动tomcat报java.lang.ClassNotFoundException: org.apache.commons.lang.xwork.StringUtils

    今天搭建了一个ssh项目环境,整合后,访问项目首页,登录不进去,控制台报错,后来调试代码后,在获取数据库数据后,返回到action时,又进入了action导致死循环,其实这里是两个问题,控制台报错如下 ...

  10. 转 java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException

    转自:http://blog.csdn.net/zb0567/article/details/7893063 java.lang.ClassNotFoundException: org.apache. ...

随机推荐

  1. JSON.stringify()和JSON.parse()的作用

    (1)JSON.stringify() 从一个对象中解析出字符串 JSON.stringify({“a”:”1”,”b”:”2”}) 结果是:”{“a”:”1”,”b”:”2”}” (2)JSON.p ...

  2. PAT A1052 Linked List Sorting (25 分)——链表,排序

    A linked list consists of a series of structures, which are not necessarily adjacent in memory. We a ...

  3. 1、话说linux内核

    1.内核和发行版的区别 到底什么是操作系统 linux.windows.android.ucos就是操作系统 操作系统本质上是一个程序,由很多个源文件构成,需要编译连接成操作系统程序(vmlinz.z ...

  4. 3.sparkSQL整合Hive

    spark SQL经常需要访问Hive metastore,Spark SQL可以通过Hive metastore获取Hive表的元数据.从Spark 1.4.0开始,Spark SQL只需简单的配置 ...

  5. Ordering Tasks

    链接 [https://vjudge.net/contest/281085#problem/D] 题意 有n个任务,有M个对先后顺序 然你输出最后的完成任务的顺序,有多种可能输出一种即可 分析 裸的拓 ...

  6. Array Division CodeForces - 808D (构造+实现)

    Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into t ...

  7. 如何优化Docker储存

    大家在使用Docker的过程中,有没有想过,Docker在本地存储镜像时把文件存储在哪里了呢?有没有对文件的总大小做一定的限制呢?能不能调整本地存储的位置及总限制大小呢?今天,我们就从这些问题入手,来 ...

  8. Eclipse启动错误JVM terminated. exit code 1解决方法

    现象: 前一天eclipse还用得好好的,但今天就不能用了,怎么回事? 解决方案: 请先参考其它网络资料:http://www.baidu.com/s?wd=eclipse+jvm+terminate ...

  9. CRM系统设计方案

    CRM系统设计方案 - 百度文库https://wenku.baidu.com/view/a34eebeb0242a8956bece473.html 服务支持http://www.uf-crm.com ...

  10. Display Hibernate SQL to console – show_sql , format_sql and use_sql_comments

    (转)灵活控制 Hibernate 的日志或 SQL 输出,以便于诊断 - CS408 - 博客园 https://www.cnblogs.com/lixuwu/p/7479496.html Disp ...