org.apache.commons.lang3工具類使用
首先需要引入依賴文章來源:http://www.zghlxwxcb.cn/news/detail-524452.html
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
常用方法如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-524452.html
/**
*org.apache.commons.lang3.StringUtils 方法
*/
@Test
void contextLoads() throws ParseException, InterruptedException {
// ************************ 字符串 ***********************************
// 有一個為空則為true
System.out.println("isAnyBlank:" + StringUtils.isAnyBlank("","4","5"));
// 全部不為空則為true
System.out.println("isNoneBlank:" + StringUtils.isNoneBlank("4","5"));
// 全部為空則為true
System.out.println("isAllBlank:" + StringUtils.isAllBlank(""," "));
// 如果字符串尚未以后綴結(jié)尾,則在字符串結(jié)尾追加后綴
System.out.println("appendIfMissing:" + StringUtils.appendIfMissing("hello","@")); // hello@
// 指定字符串不以某段字符串開頭,則自動添加開頭
System.out.println("prependIfMissing :" + StringUtils.prependIfMissing("hello","@")); // @hello
// 判斷字符串是否以什么結(jié)尾
System.out.println("endsWith:" + StringUtils.endsWith("abcd.s@","@"));
/**
* 截取字符串做一個縮減操作 固定占位符 ... 站三個寬度
* 也就是最終會有三個點,最大寬度為總共幾個包括...
* 且最大寬度最小為4,如果最大長度大于等于 源字符串則不生效相當于沒有進行操作
*/
System.out.println("abbreviate:" + StringUtils.abbreviate("測試字 符串", 6)); // abbreviate:測...
System.out.println("abbreviate:" + StringUtils.abbreviate("abcd.s@", 5)); // abbreviate:ab...
// 首字母轉(zhuǎn)大寫
System.out.println("capitalize:" + StringUtils.capitalize("abcd.s@"));
// 首字母轉(zhuǎn)小寫
System.out.println("uncapitalize:" + StringUtils.uncapitalize("abcd.s@"));
// 字符串中間填充(長度包含源字符串長度)
System.out.println("center :" + StringUtils.center("測試字 符串",8,"*"));
// 字符串左填充
System.out.println("leftPad :" + StringUtils.leftPad("測試字 符串",5,"*"));
// 字符串右填充
System.out.println("rightPad :" + StringUtils.rightPad("測試字 符串",6,"*"));
// 從字符串中刪除最后一個換行符\n、\r、\r\n
System.out.println("chomp :" + StringUtils.chomp("abcd.@569 \r"));
// 剔除字符串最后一個字符(如果最后一個是\n或\r\或\r\n也剔除掉)
System.out.println("chop :" + StringUtils.chop("abcd.@569 \r"));
// 刪除字符串中的空白全部 java 的trim只去除首尾的
System.out.println("deleteWhitespace :" + StringUtils.deleteWhitespace("測試字 符串"));
// 比較兩個字符串,并返回第二個字符串的剩余部分,從它與第一個不同的地方開始
System.out.println("difference :" + StringUtils.difference("abcd.s@","abcd.@569 \r"));
// 獲得多個字符串相同的開頭內(nèi)容,接收參數(shù)為多個字符串
System.out.println("getCommonPrefix :" + StringUtils.getCommonPrefix("abc","ac","abcd")); // a
// 獲取字符串中的數(shù)字
System.out.println("getDigits :" + StringUtils.getDigits("ckds123456cs6.9dkcm")); // 12345669
// 給字符串數(shù)組添加知道分隔符
System.out.println("joinWith :" + StringUtils.joinWith(",","abc25","abc")); // abc25,abc
// 字符串截取相當于substring,不同在于當截取字符超過源字符串長度不會報錯
System.out.println("mid :" + StringUtils.mid("asdfg",1,8)); // sdfg
// 用另一個字符串覆蓋字符串的一部分(指定區(qū)域)
System.out.println("overlay :" + StringUtils.overlay("abcdef", "zzzz", 2, 4)); // abzzzzef
//替換字符串內(nèi)容……等同于java 原生 replace()和 replaceFirst()
System.out.println("replace :" + StringUtils.replace("aba", "a", "z")); // zbz
System.out.println("replaceOnce :" + StringUtils.replaceOnce("aba", "a", "z")); // zba
// //統(tǒng)計一字符串在另一字符串中出現(xiàn)次數(shù)
System.out.println("countMatches :" + StringUtils.countMatches("dcabcfccrt","c")); // 4
// 刪除字符串中的指定內(nèi)容
System.out.println("remove :" + StringUtils.remove("abcdef","c")); // abdef
// 刪除字符串中結(jié)尾(滿足是以某段內(nèi)容結(jié)尾)
System.out.println("removeEnd :" + StringUtils.removeEnd("abcdef","ef")); // abcd
// 刪除字符串中開頭(滿足是以某段內(nèi)容開始)
System.out.println("removeStart :" + StringUtils.removeStart("abcdef","a")); // bcdef
// //重復(fù)字符/字符串
System.out.println("repeat :" + StringUtils.repeat("abcdef",5)); // abcdefabcdefabcdefabcdefabcdef
// 字符串翻轉(zhuǎn)
System.out.println("reverse :" + StringUtils.reverse("abcdef")); // fedcba
// 根據(jù)指定分隔符進行反轉(zhuǎn),分隔符之間的字符不進行反轉(zhuǎn)
System.out.println("reverseDelimited :" + StringUtils.reverseDelimited("abc,def",',')); // def,abc
// 將字符串后面指定個數(shù)的字符翻轉(zhuǎn)到前面
System.out.println("rotate :" + StringUtils.rotate("abc,defgfd",3)); // gfdabc,def
// 截取指定字符串前面的內(nèi)容
System.out.println("substringBefore :" + StringUtils.substringBefore("abcba", "b")); // a
// 截取最后指定字符串前面的內(nèi)容
System.out.println("substringBeforeLast :" + StringUtils.substringBeforeLast("abcba", "b")); // abc
// 截取指定字符串后面的內(nèi)容
System.out.println("substringAfter :" + StringUtils.substringAfter("abcba", "b")); // cab
// 截取最后指定字符串后面的內(nèi)容
System.out.println("substringAfterLast :" + StringUtils.substringAfterLast("abcba", "b")); // a
// 截取指定字符串中間的內(nèi)容
System.out.println("substringBetween :" + StringUtils.substringBetween("tagabctag", "tag")); // abc
System.out.println("substringBetween :" + StringUtils.substringBetween("yabczyabcz", "y", "z")); // abc
// 截斷字符串 最大寬度科超過源字符串長度不會報錯
System.out.println("truncate :" + StringUtils.truncate("abcba",6)); // abcba
// 判斷字符串數(shù)字
System.out.println("isNumeric :" + StringUtils.isNumeric("12 3")); // false
System.out.println("isNumericSpace :" + StringUtils.isNumericSpace("12 3")); // true
// 判斷字符串是否只有字母和數(shù)字組成 漢字可通過
System.out.println("isAlphanumeric :" + StringUtils.isAlphanumeric("12xs好j3")); // true
// 判斷字符串是否只有字母和數(shù)字和空格組成 漢字可通過
System.out.println("isAlphanumericSpace :" + StringUtils.isAlphanumericSpace("12xs 好j3")); // true
// 字符串截取,不會忽略空數(shù)據(jù)
System.out.println("splitByWholeSeparator :" + Arrays.asList(StringUtils.splitByWholeSeparator("12x,s 好j3,,", ","))); // [12x, s 好j3, ]
// ************************ 數(shù)字 ***********************************
// //判斷字符串是否全是整數(shù)
System.out.println("isDigits :" + NumberUtils.isDigits("12.3")); // false
//從數(shù)組中選出最大值
System.out.println("max :" + NumberUtils.max(1, 2, 3, 4)); // 4
//從數(shù)組中選出最小值
System.out.println("min :" + NumberUtils.min(1, 2, 3, 4)); // 1
//將一個字符串轉(zhuǎn)換為int類型,失敗返回0
System.out.println("toInt :" + NumberUtils.toInt("123")); // 123
//將一個字符串轉(zhuǎn)換為int類型,失敗返回自定義
System.out.println("toDouble :" + NumberUtils.toDouble("123.1", 0)); // 123.1
//將一個字符串轉(zhuǎn)換為BigDecimal,自定義小數(shù)位數(shù),自定義舍入模式,默認保留2位小數(shù),舍入模式為RoundingMode.HALF_EVEN
System.out.println("toScaledBigDecimal :" + NumberUtils.toScaledBigDecimal("2.1", 2, RoundingMode.HALF_UP)); // 2.10
// ,判斷一個字符串是否可轉(zhuǎn)化為數(shù)字isParsable / isCreatable
System.out.println("isParsable :" + NumberUtils.isParsable("3.26")); // true
System.out.println("isCreatable :" + NumberUtils.isCreatable("3.26")); // true
// ************************ 數(shù)組 ***********************************
//創(chuàng)建數(shù)組
String[] array = ArrayUtils.toArray("1", "2");
System.out.println("判斷數(shù)組中是否包含某一對象 :" + ArrayUtils.contains(array, "33"));
// ************************ 日期工具 ***********************************
System.out.println("Date 轉(zhuǎn)化為字符串 :" + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
System.out.println("字符串 轉(zhuǎn) Date :" + DateUtils.parseDate("2022-12-19 22:00:00", "yyyy-MM-dd HH:mm:ss"));
Date now = new Date();
Date addDays = DateUtils.addDays(now, 1);
System.out.println("Date 加 1 天 :" + addDays);
Date addMinutes = DateUtils.addMinutes(now, 30);
System.out.println("Date 加 30 分鐘 :" + addMinutes);
Date addSeconds = DateUtils.addSeconds(now, -180);
System.out.println("Date 減去 180 秒 :" + addSeconds);
System.out.println("http:// 判斷是否 為 同一天 :" + DateUtils.isSameDay(addDays, addMinutes));
// 過濾時分秒,若 now 為 2020-05-07 22:13:00 調(diào)用 truncate 方法以后
// 返回時間為 2020-05-07 00:00:00
Date truncate = DateUtils.truncate(now, Calendar.DATE);
// ************************ 時間計算 ***********************************
// 創(chuàng)建之后立刻計時
StopWatch watch = StopWatch.createStarted();
// 若想主動開始計時,創(chuàng)建計時器,調(diào)用 start 方法開始計時
StopWatch watch1 = StopWatch.create();
watch1.start();
// 模擬其他代碼耗時
TimeUnit.SECONDS.sleep(2L);
System.out.println("統(tǒng)計從開始到現(xiàn)在運行時間:" + watch.getTime() + "ms"); //2000ms
TimeUnit.SECONDS.sleep(2L);
watch.split();
System.out.println("從start到此刻為止的時間:" + watch.getTime()); //4000
System.out.println("從開始到第一個切入點運行時間:" + watch.getSplitTime()); //4000
Thread.sleep(1000);
watch.split();
System.out.println("從開始到第二個切入點運行時間:" + watch.getSplitTime()); //5001
watch.reset(); //重置后必須使用start方法
watch.start();
Thread.sleep(1000);
System.out.println("重新開始后到當前運行時間是:" + watch.getTime()); //1000
watch.suspend(); //暫停
Thread.sleep(6000); //模擬暫停6秒鐘
watch.resume(); //上面suspend,這里要想重新統(tǒng)計,需要恢復(fù)一下
System.out.println("恢復(fù)后執(zhí)行的時間是:" + watch.getTime()); //1000 注意此時這個值還是1000
watch.stop();
System.out.println("花費的時間 ==>" + watch.getTime() + "ms"); //1000ms
System.out.println("花費的時間 ==>" + watch.getTime(TimeUnit.SECONDS) + "s"); //1s 可以直接轉(zhuǎn)成s
}
到了這里,關(guān)于org.apache.commons.lang3工具類使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!