部分轉載至: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 nul
l
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 //去除字符串中的"\n", "\r", or "\r\n" 21 StringUtils.chomp("abc\r\n");//---"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