zoukankan      html  css  js  c++  java
  • 关于Apache Commons-Lang的总结

    部分转载至:http://linhongyu.blog.51cto.com/6373370/1553329

    一、前言

        Java码农不识Apache,敲尽一生也枉然。旗下的开源项目众多,各个都是吊炸天。今日且说Commons,轻轻点击此链接进入Apache Commons主页,Logging、Pool、Net、ONGL、EL、IO、DBCP、Email、Collection、Lang……等等项目中常用到的包。而这篇文章的主角Lang则是我们最常用的工具作为jdk的补充,怎能不去详细探究一番!

    二、字符串的处理类(StringUtils)

        org.apache.commons.lang3.StringUtils 继承Object,Operations on String that are null safe。所谓的null safe就是对String进行操作不会出现NullPointerException异常,很实用有没有!以后再也不怕到处出现空指针异常了。先看看官方文档中这个类都有些什么方法:

      这些方法基本上看方法名,就能猜出它大概的作用了。

      1         //缩短到某长度,用...结尾.其实就是(substring(str, 0, max-3) + "...")
      2         //public static String abbreviate(String str,int maxWidth)
      3         StringUtils.abbreviate("abcdefg", 6);// ---"abc..."
      4          
      5         //字符串结尾的后缀是否与你要结尾的后缀匹配,若不匹配则添加后缀
      6         StringUtils.appendIfMissing("abc","xyz");//---"abcxyz"
      7         StringUtils.appendIfMissingIgnoreCase("abcXYZ","xyz");//---"abcXYZ"
      8          
      9         //首字母大小写转换
     10         StringUtils.capitalize("cat");//---"Cat"
     11         StringUtils.uncapitalize("Cat");//---"cat"
     12          
     13         //字符串扩充至指定大小且居中(若扩充大小少于原字符大小则返回原字符,若扩充大小为 负数则为0计算 )
     14         StringUtils.center("abcd", 2);//--- "abcd"
     15         StringUtils.center("ab", -1);//--- "ab"
     16         StringUtils.center("ab", 4);//---" ab "
     17         StringUtils.center("a", 4, "yz");//---"yayz"
     18         StringUtils.center("abc", 7, "");//---"  abc  "
     19          
     20         //去除字符串中的"
    ", "
    ", or "
    "
     21         StringUtils.chomp("abc
    ");//---"abc"
     22          
     23         //判断一字符串是否包含另一字符串
     24         StringUtils.contains("abc", "z");//---false
     25         StringUtils.containsIgnoreCase("abc", "A");//---true
     26          
     27         //统计一字符串在另一字符串中出现次数
     28         StringUtils.countMatches("abba", "a");//---2
     29          
     30         //删除字符串中的梭有空格
     31         StringUtils.deleteWhitespace("   ab  c  ");//---"abc"
     32          
     33         //比较两字符串,返回不同之处。确切的说是返回第二个参数中与第一个参数所不同的字符串
     34         StringUtils.difference("abcde", "abxyz");//---"xyz"
     35          
     36         //检查字符串结尾后缀是否匹配
     37         StringUtils.endsWith("abcdef", "def");//---true
     38         StringUtils.endsWithIgnoreCase("ABCDEF", "def");//---true
     39         StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"});//---true
     40          
     41         //检查起始字符串是否匹配
     42         StringUtils.startsWith("abcdef", "abc");//---true
     43         StringUtils.startsWithIgnoreCase("ABCDEF", "abc");//---true
     44         StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"});//---true
     45          
     46         //判断两字符串是否相同
     47         StringUtils.equals("abc", "abc");//---true
     48         StringUtils.equalsIgnoreCase("abc", "ABC");//---true
     49          
     50         //比较字符串数组内的所有元素的字符序列,起始一致则返回一致的字符串,若无则返回""
     51         StringUtils.getCommonPrefix(new String[] {"abcde", "abxyz"});//---"ab"
     52          
     53         //正向查找字符在字符串中第一次出现的位置
     54         StringUtils.indexOf("aabaabaa", "b");//---2
     55         StringUtils.indexOf("aabaabaa", "b", 3);//---5(从角标3后查找)
     56         StringUtils.ordinalIndexOf("aabaabaa", "a", 3);//---1(查找第n次出现的位置)
     57          
     58         //反向查找字符串第一次出现的位置
     59         StringUtils.lastIndexOf("aabaabaa", 'b');//---5
     60         StringUtils.lastIndexOf("aabaabaa", 'b', 4);//---2
     61         StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 2);//---1
     62          
     63         //判断字符串大写、小写
     64         StringUtils.isAllUpperCase("ABC");//---true
     65         StringUtils.isAllLowerCase("abC");//---false
     66          
     67         //判断是否为空(注:isBlank与isEmpty 区别)
     68         StringUtils.isBlank(null);StringUtils.isBlank("");StringUtils.isBlank(" ");//---true
     69         StringUtils.isNoneBlank(" ", "bar");//---false
     70          
     71         StringUtils.isEmpty(null);StringUtils.isEmpty("");//---true
     72         StringUtils.isEmpty(" ");//---false
     73         StringUtils.isNoneEmpty(" ", "bar");//---true
     74          
     75         //判断字符串数字
     76         StringUtils.isNumeric("123");//---false
     77         StringUtils.isNumeric("12 3");//---false (不识别运算符号、小数点、空格……)
     78         StringUtils.isNumericSpace("12 3");//---true
     79          
     80         //数组中加入分隔符号
     81         //StringUtils.join([1, 2, 3], ';');//---"1;2;3"
     82          
     83         //大小写转换
     84         StringUtils.upperCase("aBc");//---"ABC"
     85         StringUtils.lowerCase("aBc");//---"abc"
     86         StringUtils.swapCase("The dog has a BONE");//---"tHE DOG HAS A bone"
     87          
     88         //替换字符串内容……(replacePattern、replceOnce)
     89         StringUtils.replace("aba", "a", "z");//---"zbz"
     90         StringUtils.overlay("abcdef", "zz", 2, 4);//---"abzzef"(指定区域)
     91         StringUtils.replaceEach("abcde", new String[]{"ab", "d"},
     92                 new String[]{"w", "t"});//---"wcte"(多组指定替换ab->w,d->t)
     93          
     94         //重复字符
     95         StringUtils.repeat('e', 3);//---"eee"
     96          
     97         //反转字符串
     98         StringUtils.reverse("bat");//---"tab"
     99          
    100         //删除某字符
    101         StringUtils.remove("queued", 'u');//---"qeed"
    102          
    103         //分割字符串
    104         StringUtils.split("a..b.c", '.');//---["a", "b", "c"]
    105         StringUtils.split("ab:cd:ef", ":", 2);//---["ab", "cd:ef"]
    106         StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 2);//---["ab", "cd-!-ef"]
    107         StringUtils.splitByWholeSeparatorPreserveAllTokens("ab::cd:ef", ":");//-["ab"," ","cd","ef"]
    108          
    109         //去除首尾空格,类似trim……(stripStart、stripEnd、stripAll、stripAccents)
    110         StringUtils.strip(" ab c ");//---"ab c"
    111         StringUtils.stripToNull(null);//---null
    112         StringUtils.stripToEmpty(null);//---""
    113          
    114         //截取字符串
    115         StringUtils.substring("abcd", 2);//---"cd"
    116         StringUtils.substring("abcdef", 2, 4);//---"cd"
    117          
    118         //left、right从左(右)开始截取n位字符
    119         StringUtils.left("abc", 2);//---"ab"
    120         StringUtils.right("abc", 2);//---"bc"
    121         //从第n位开始截取m位字符       n  m
    122         StringUtils.mid("abcdefg", 2, 4);//---"cdef"
    123          
    124         StringUtils.substringBefore("abcba", "b");//---"a"
    125         StringUtils.substringBeforeLast("abcba", "b");//---"abc"
    126         StringUtils.substringAfter("abcba", "b");//---"cba"
    127         StringUtils.substringAfterLast("abcba", "b");//---"a"
    128          
    129         StringUtils.substringBetween("tagabctag", "tag");//---"abc"
    130         StringUtils.substringBetween("yabczyabcz", "y", "z");//---"abc"

    三、其它类简介

        RandomStringUtils:

     //随机生成n位数数字
            RandomStringUtils.randomNumeric(n);
            //在指定字符串中生成长度为n的随机字符串
            RandomStringUtils.random(n, "abcdefghijk");
            //指定从字符或数字中生成随机字符串
            System.out.println(RandomStringUtils.random(n, true, false));  
            System.out.println(RandomStringUtils.random(n, false, true));

    NumberUtils:

     //从数组中选出最大值
            NumberUtils.max(new int[] { 1, 2, 3, 4 });//---4
            //判断字符串是否全是整数
            NumberUtils.isDigits("153.4");//--false
            //判断字符串是否是有效数字
            NumberUtils.isNumber("0321.1");//---false    

    ArrayUtils:

    //创建数组
            String[] array = ArrayUtils.toArray("1", "2");
            //判断两个数据是否相等,如果内容相同, 顺序相同 则返回 true
            ArrayUtils.isEquals(arr1,arr2);
            //判断数组中是否包含某一对象
            ArrayUtils.contains(arr, "33");
            //二维数组转换成MAP
            Map map = ArrayUtils.toMap(new String[][] { 
                    { "RED", "#FF0000" }, { "GREEN", "#00FF00" }, { "BLUE", "#0000FF" } });

      DateUtils:

            //日期加n天
            DateUtils.addDays(new Date(), n);
            //判断是否同一天
            DateUtils.isSameDay(date1, date2);
            //字符串时间转换为Date
            DateUtils.parseDate(str, parsePatterns);

    四、结语

        本文只是简单的介绍了commons-lang中的一些常用工具类,还有许多挺实用的就不一一列举。还是要自己去查阅文档试用了才能体会到它的简便。

     >>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    http://janwer.iteye.com/blog/148313

  • 相关阅读:
    洛谷 P3355 骑士共存问题
    Redis 安装
    Java 集合:(十八) Map接口
    并发编程——Java版【目录】
    Java 集合:(十七) Queue 常用接口:BlockingQueue 子接口
    Java 集合:(十六) Queue 常用接口:Deque 子接口
    Java 集合:(十五) Queue 子接口
    Java 集合:(番外篇一) ArrayList线程不安全性
    第十三章:StringTable
    Java 集合:(十三) Set实现类:LinkedHashSet
  • 原文地址:https://www.cnblogs.com/DreamDrive/p/4282323.html
Copyright © 2011-2022 走看看