一、StringUtils
1、Spring提供的StringUtils
/** * Spring自帶的StringUtils */ @Test void testStringUtils01(){ boolean b = StringUtils.hasText(" "); //可用 boolean empty = StringUtils.isEmpty(" "); //可用 System.out.println(b); //false 認為沒有文本 我們想要的 System.out.println(empty); //false 認為不為空 空格不是null 也是我們想要的 //測試結果 不是想要的結果 String[] split = StringUtils.split("ni shi ge cao bao", " "); assert split != null; for (String s:split) { System.out.println(s); } //測試結果 單詞之間的空格是一個沒有問題,多個空格就報錯 String[] strings = StringUtils.tokenizeToStringArray("ni shi cao bao", " "); for (String s : strings) { System.out.println(s); } }
2、Commons-lang3 提供的StringUtils工具類
/** * commons-lang3 提供的StringUtils工具類 */ @Test void testStringUtils02() { boolean notBlank = StringUtils.isNotBlank(" "); boolean blank = StringUtils.isBlank(" "); System.out.println(notBlank);//false System.out.println(blank);//true boolean empty = StringUtils.isEmpty(" "); System.out.println(empty);//false boolean contains1 = StringUtils.contains("你是哪里人", "你"); boolean contains2 = StringUtils.contains("你是哪里人", "哪里人"); System.out.println(contains1);//true System.out.println(contains2);//true String replace1 = StringUtils.replace("你是哪里人 ,你是哪里人", "哪里人", "我的心上人"); System.out.println(replace1);//你是我的心上人 ,你是我的心上人 String replace2 = StringUtils.replaceOnce("你是哪里人 ,你是哪里人", "你是哪里人", "心上人"); System.out.println(replace2);//心上人 ,你是哪里人 int compare1 = StringUtils.compare("you are in my heart", "you are in my heart"); int compare2 = StringUtils.compareIgnoreCase("you are in my heart", "you are iN my heart"); System.out.println(compare1); //0 System.out.println(compare2); //0 boolean b = StringUtils.startsWith("you are in my heart", "you"); System.out.println(b);//true }
二、DateUtils
1、Commons-lang3 提供的DateUtils工具類
/** * DateUtils */ @Test void testDate(){ Date date = new Date(); Date date1 = DateUtils.addDays(date, 1); Date date2 = DateUtils.addHours(date1, 10); boolean sameDay = DateUtils.isSameDay(date1, date2); Date date3 = DateUtils.setYears(date1, 2030); System.out.println(sameDay); System.out.println(date3); }
三、CollectionUtils(以下都是Spring框架提供的)
1、比較集合中的元素
/** * 比較兩個集合中的元素 */ @Test void contextLoads() { List<String> list1 = new ArrayList<String>(); list1.add("1"); list1.add("2"); List<String> list2 = new ArrayList<>(); list2.add("1"); list2.add("3"); list2.add("4"); //並集 Collection diff1 = CollectionUtils.union(list1, list2); System.out.println(diff1.toString()); //[1, 2, 3, 4] //交集 Collection diff2 = CollectionUtils.intersection(list2, list1); System.out.println(diff2.toString()); //[1] //交集的補集 Collection diff3 = CollectionUtils.disjunction(list1, list2); System.out.println(diff3); //[2, 3, 4] //list1與list2的差 Collection diff4 = CollectionUtils.subtract(list1, list2); System.out.println(diff4); //[2] //list2與list1的差 Collection diff5 = CollectionUtils.subtract(list2, list1); System.out.println(diff5); //[3, 4] }
2、是否為空判斷
/** * CollectionUtils */ @Test void testCollections(){ //判斷HashMap是否為空 HashMap<Object, Object> objectObjectHashMap = new HashMap<>(); boolean empty = CollectionUtils.isEmpty(objectObjectHashMap); System.out.println(empty);//true //判斷ArrayList是否為空 ArrayList<Object> objects = new ArrayList<>(); boolean empty1 = CollectionUtils.isEmpty(objects); System.out.println(empty1);//true //數組轉集合 String[] a ={"wo","he","ni"}; List list = CollectionUtils.arrayToList(a); Object o2 = CollectionUtils.lastElement(list); System.out.println(list);//[wo, he, ni] System.out.println(o2);//ni }
四、AlternativeJdkIdGenerator(UUID工具類生成)
/** * UUID工具類生成 */ @Test void testAlternativeJdkIdGenerator(){ AlternativeJdkIdGenerator a=new AlternativeJdkIdGenerator(); UUID uuid = a.generateId(); String id = StringUtils.replace(uuid.toString(), "-", ""); System.out.println(id);//生成去掉“-”的32位uuid 例如:b079c3d726b5b7ae6d432d2e2a0831b3 }
五、PathMatcher(路徑匹配器)
/** * PathMatcher */ @Test void testPathMatcher(){ PathMatcher pathMatcher = new AntPathMatcher(); System.out.println(); // 精確匹配 System.out.println(pathMatcher.match("/test", "/test"));//true System.out.println(pathMatcher.match("test", "/test"));//false //測試占位符? System.out.println(pathMatcher.match("t?st", "test"));//true System.out.println(pathMatcher.match("te??", "test"));//true System.out.println(pathMatcher.match("tes?", "tes"));//false System.out.println(pathMatcher.match("tes?", "testt"));//false //測試通配符* System.out.println(pathMatcher.match("*", "test"));//true System.out.println(pathMatcher.match("test*", "test"));//true System.out.println(pathMatcher.match("test/*", "test/Test"));//true System.out.println(pathMatcher.match("*.*", "test."));//true System.out.println(pathMatcher.match("*.*", "test.test.test"));//true System.out.println(pathMatcher.match("test*", "test/")); //注意這里是false 因為路徑不能用*匹配 System.out.println(pathMatcher.match("test*", "test/t")); //false這同理 System.out.println(pathMatcher.match("test*aaa", "testblaaab")); //這個是false 因為最后一個b無法匹配了 前面都是能匹配成功的 //測試通配符** 匹配多級URL System.out.println(pathMatcher.match("/*/**", "/testing/testing"));//true System.out.println(pathMatcher.match("/**/*", "/testing/testing"));//true System.out.println(pathMatcher.match("/bla/**/bla", "/bla/testing/testing/bla/bla")); //這里也是true哦 System.out.println(pathMatcher.match("/bla*bla/test", "/blaXXXbl/test"));//false System.out.println(pathMatcher.match("/????", "/bala/bla"));//false System.out.println(pathMatcher.match("/**/*bla", "/bla/bla/bla/bbb"));//false System.out.println(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing/"));//true System.out.println(pathMatcher.match("/*bla*/**/bla/*", "/XXXblaXXXX/testing/testing/bla/testing"));//true System.out.println(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing"));//true System.out.println(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing.jpg"));//true System.out.println(pathMatcher.match("/foo/bar/**", "/foo/bar"));//true //這個需要特別注意:{}里面的相當於Spring MVC里接受一個參數一樣,所以任何東西都會匹配的 System.out.println(pathMatcher.match("/{bla}.*", "/testing.html"));//true System.out.println(pathMatcher.match("/{bla}.htm", "/testing.html")); //這樣就是false了 }
