commons-lang
今天在编码的过程中,对于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的更多相关文章
- Apache Commons Lang
http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/package- ...
- 让时间处理简单化 【第三方扩展类库org.apache.commons.lang.time】
JAVA的时间日期处理一直是一个比较复杂的问题,大多数程序员都不能很轻松的来处理这些问题.首先Java中关于时间的类,从 JDK 1.1 开始,Date的作用很有限,相应的功能已由Calendar与D ...
- 关于出现 org.apache.commons.lang.exception.NestableRuntimeException的解决方法
最近做服务端和客户端之间的访问,出现了 org.apache.commons.lang.exception.NestableRuntimeException等状况.实在令人头大,翻到了一个很好的帖子说 ...
- org.apache.commons.lang.StringUtils中常用的方法
org.apache.commons.lang.StringUtils中常用的方法,这里主要列举String中没有,且比较有用的方法: 1. 检查字符串是否为空: static boolean isB ...
- java转换json需要导入的jar包,org/apache/commons/lang/exception/NestableRuntimeException
缺少相应jar包都会有异常,根据异常找jar包导入...... 这里我说下lang包,因为这个包我找了好半天: 我用的是: commons-lang3-3.1.jar 出现异常: jav ...
- org.apache.commons.lang.StringUtils类
org.apache.commons.lang.StringUtils类 本文摘自:(http://www.blogjava.net/japper/archive/2012/05/23/378946. ...
- 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 ...
- java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils
java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils Caused by: java.lang.ClassNotFou ...
- ssh整合启动tomcat报java.lang.ClassNotFoundException: org.apache.commons.lang.xwork.StringUtils
今天搭建了一个ssh项目环境,整合后,访问项目首页,登录不进去,控制台报错,后来调试代码后,在获取数据库数据后,返回到action时,又进入了action导致死循环,其实这里是两个问题,控制台报错如下 ...
- 转 java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
转自:http://blog.csdn.net/zb0567/article/details/7893063 java.lang.ClassNotFoundException: org.apache. ...
随机推荐
- wxWidgets 在 Linux 下开发环境配置
本文基于 CodeBlocks (16.0.1) 和 wxWidgets (3.0.2) 搭建 Linux 下 GUI 开发环境. 1. 安装 CodeBlocks Ubuntu 默认的源当前 Cod ...
- Vue学习-基本指令
一.关于vue介绍:https://mp.weixin.qq.com/s?__biz=MzUxMzcxMzE5Ng==&mid=2247485737&idx=1&sn=14fe ...
- CentOS7.4 ISCSI
试验机配置: cat /etc/centos-release CentOS Linux release 7.4.1708 (Core) uname -r 3.10.0-693.el7.x86_64 所 ...
- npm太慢, 淘宝npm镜像使用方法
淘宝 npm 地址: http://npm.taobao.org/ 如何使用 有很多方法来配置npm的registry地址,下面根据不同情境列出几种比较常用的方法.以淘宝npm镜像举例: 1.临时使用 ...
- C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 严格的用户账户审核功能
整个集团有几万个用户,一个个用户添加是不现实的,只有每个公司的系统管理员添加.或者用户申请帐户,然后有相应的管理员审核,才会更准确一些. 每个公司.分公司.部门的账户情况只有所在公司的管理员是最清楚的 ...
- 二、截取字符串长度(css方式)
只针对谷歌 width: 100%; //height: 58px; overflow:hidden; text-overflow:ellipsis; display: -webkit-box; -w ...
- 插入排序专题 直接插入 折半 希尔shell
1.直接插入排序 分析:a[n]有n个元素 a[0...n-1] 从 i=1...n-1 a[i]依次与 a[0...n-2]数字进行比较 发现后面的数字大于前面的数字交换位置,每一次比较,与 ...
- Codeforces Round #486 (Div. 3)-B. Substrings Sort
B. Substrings Sort time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 1168: mxh对lfx的询问(前缀和+素数表)
题目描述: AS WE ALL KNOW, lfx是咱们组的神仙,但是mxh想考一考lfx一个简单的问题,以此看一下lfx到底是不是神仙.但是lfx要准备补考,于是请你来帮忙回答问题: 给定一个整数N ...
- agora入门案例
一,下载agora的WebSDK 二,运行index.html 三,输入appID 1.找到appID 2.页面输入appID,查看效果