zoukankan      html  css  js  c++  java
  • 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-srcsrc est

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

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

  • 相关阅读:
    windows系统切换jdk,修改java_home无效情况
    Cannot instantiate interface org.springframework.context.ApplicationListener
    MySQL分组查询获取每个学生前n条分数记录(分组查询前n条记录)
    ASP.NET Web API 使用Swagger生成在线帮助测试文档,支持多个GET
    EF TO MYSQL 无法查询中文的解决方法
    HttpWebRequest post请求获取webservice void数据信息
    This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms. 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分 解决方案
    MySQL 5.7.13解压版安装记录 mysql无法启动教程
    C# udpclient 发送数据断网后自动连接的方法
    汽车XX网站秒杀抢购代码
  • 原文地址:https://www.cnblogs.com/parkdifferent/p/5850819.html
Copyright © 2011-2022 走看看