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);//3
    }

    @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);  //4
    }

    @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

时间: 2024-12-19 07:08:18

commons-lang的相关文章

Apache commons lang工具类学习笔记(2)--StringUtils

StringUtils工具类具有对String具有简单而强大的处理能力,从检查空串到分割字符串,到生成格式化的字符串,使用都很方便简洁,能减少很多代码量; 详细的使用方法可以参考下面的例子或者官方的API(http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringUtils.html#isAlpha(java.lang.CharSequence)) packa

org.apache.commons.lang.exception.NestableRuntimeException等缺少jar包的解决办法

最近做服务端和客户端之间的访问,出现了 org.apache.commons.lang.exception.NestableRuntimeException等状况.实在令人头大,翻到了一个很好的帖子说明了这个问题. 原文网址如下:http://blog.csdn.net/zb0567/article/details/7893063 为方便更多的人解决这个问题,现将原文贴出 Java.lang.ClassNotFoundException: org.apache.commons.lang.exce

Caused by: java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException

转载:http://www.tuicool.com/articles/Vvia6f 缺少相应jar包都会有异常,根据异常找jar包导入...... 这里我说下lang包,因为这个包我找了好半天: 我用的是: commons-lang3-3.1.jar  出现异常: java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException 可以看出是因为缺少jar包,但是很明显我已经导入了,

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

遇到这种问题是因为jar包引入不全,完整的应该包含: commons-beanutils-1.8.3.jar commons-lang-2.5.jar ezmorph-1.0.6.jar json-lib-2.3-jdk15.jar 但是又出现如下问题: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory 导入commons-logging-1.1.1.jar 又出现如下问题: java.lang.NoC

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

最近做服务端和客户端之间的访问,出现了 org.apache.commons.lang.exception.NestableRuntimeException等状况.实在令人头大,翻到了一个很好的帖子说明了这个问题. 原文网址如下:http://blog.csdn.net/zb0567/article/details/7893063 为方便更多的人解决这个问题,现将原文贴出 Java.lang.ClassNotFoundException: org.apache.commons.lang.exce

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

今天搭建了一个ssh项目环境,整合后,访问项目首页,登录不进去,控制台报错,后来调试代码后,在获取数据库数据后,返回到action时,又进入了action导致死循环,其实这里是两个问题,控制台报错如下: 2015-4-27 20:57:56 org.apache.catalina.core.StandardWrapperValve invoke严重: Servlet.service() for servlet default threw exceptionjava.lang.ClassNotFo

【Apache Commons Lang】StopWatch任务执行时间监视器

StopWath是apache commons lang包下的一个任务执行时间监视器 主要方法:     start();     //开始计时     split();     //设置split点     getSplitTime();  //获取从start 到 最后一次split的时间     reset();     //重置计时     suspend();     //暂停计时, 直到调用resume()后才恢复计时     resume();      //恢复计时     st

org.apache.commons.lang.StringUtils 中 Join 函数

转自 http://my.oschina.net/zenglingfan/blog/134872 写代码的时候,经常会碰到需要把一个List中的每个元素,按逗号分隔转成字符串的需求,以前是自己写一段比较难看的代码,先把字符串拼出来,再把最后面多余的逗号去掉:虽然功能可以实现,但总觉得最后加的那一步操作很没有必要: public static String join(List<String> list, String seperator){ if(list.isEmpty()){ return

java开发_org.apache.commons.lang.StringUtils工具类源码

package org.apache.commons.lang; 18 19 import java.util.ArrayList; 20 import java.util.Collection; 21 import java.util.Iterator; 22 import java.util.List; 23 import java.util.Locale; 24 25 import org.apache.commons.lang.text.StrBuilder; 26 27 /** 28

Java_异常_01_org.apache.commons.lang.exception.NestableRuntimeException

异常信息: The type org.apache.commons.lang.exception.NestableRuntimeException cannot be resolved. It is indirectly referenced from required .class files 原因:apache.commons.lang的jar包出现问题.可能是少了jar包,可能是jar包版本冲突. 解决过程:导入commons-lang3-3.6.jar还是出现这个问题,换成commons