Java實(shí)用工具庫(kù)commons-lang3的使用
Java實(shí)用工具庫(kù)commons-lang3
Apache Commons Lang 是一個(gè)流行的 Java 實(shí)用工具庫(kù),其中 commons-lang3
是其最新的主流版本,用于增強(qiáng) Java 核心功能,特別是對(duì) java.lang
包的擴(kuò)展。
以下是 commons-lang3
的功能概述,以及如何使用其中的一些工具(如 RegExUtils
)。
Maven 依賴
在使用 commons-lang3
之前,需要在項(xiàng)目中添加 Maven 依賴:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> <!-- 或最新版本 --> </dependency>
主要工具與功能
1. StringUtils
- 提供對(duì)字符串操作的各種實(shí)用方法。
import org.apache.commons.lang3.StringUtils; public class StringUtilsExample { public static void main(String[] args) { String str = " Hello World "; // 判斷字符串是否為空或空白 System.out.println(StringUtils.isBlank(str)); // false // 刪除兩端空格 System.out.println(StringUtils.trim(str)); // "Hello World" // 反轉(zhuǎn)字符串 System.out.println(StringUtils.reverse(str)); // " dlroW olleH " } }
2. RegExUtils
- 用于處理正則表達(dá)式的高級(jí)工具。
import org.apache.commons.lang3.RegExUtils; public class RegExUtilsExample { public static void main(String[] args) { String text = "key1:value1, key2:value2, key3:value3"; // 替換冒號(hào)為下劃線 String result = RegExUtils.replaceAll(text, ":", "_"); System.out.println(result); // "key1_value1, key2_value2, key3_value3" } }
3. NumberUtils
- 提供數(shù)字操作的工具方法。
import org.apache.commons.lang3.math.NumberUtils; public class NumberUtilsExample { public static void main(String[] args) { // 判斷是否是數(shù)字 System.out.println(NumberUtils.isCreatable("123")); // true System.out.println(NumberUtils.isCreatable("12.3")); // true System.out.println(NumberUtils.isCreatable("abc")); // false // 找到數(shù)組中的最大值 int[] numbers = {1, 2, 3, 4, 5}; System.out.println(NumberUtils.max(numbers)); // 5 } }
4. DateUtils
- 提供日期和時(shí)間操作的工具。
import org.apache.commons.lang3.time.DateUtils; import java.text.ParseException; import java.util.Date; public class DateUtilsExample { public static void main(String[] args) throws ParseException { String dateStr = "2025-01-01"; // 解析日期 Date date = DateUtils.parseDate(dateStr, "yyyy-MM-dd"); System.out.println(date); // 添加天數(shù) Date newDate = DateUtils.addDays(date, 5); System.out.println(newDate); } }
5. RandomStringUtils
- 生成隨機(jī)字符串的工具。
import org.apache.commons.lang3.RandomStringUtils; public class RandomStringUtilsExample { public static void main(String[] args) { // 生成隨機(jī)字母字符串 String randomAlphabetic = RandomStringUtils.randomAlphabetic(10); System.out.println(randomAlphabetic); // 生成隨機(jī)數(shù)字字符串 String randomNumeric = RandomStringUtils.randomNumeric(10); System.out.println(randomNumeric); } }
6. ObjectUtils
- 提供對(duì)對(duì)象操作的工具。
import org.apache.commons.lang3.ObjectUtils; public class ObjectUtilsExample { public static void main(String[] args) { String str = null; // 如果對(duì)象為 null,提供默認(rèn)值 System.out.println(ObjectUtils.defaultIfNull(str, "Default Value")); // "Default Value" // 判斷多個(gè)對(duì)象是否都不為空 System.out.println(ObjectUtils.allNotNull("A", "B", "C")); // true System.out.println(ObjectUtils.allNotNull("A", null, "C")); // false } }
總結(jié)
commons-lang3
提供了一系列增強(qiáng) Java 核心功能的工具類,常用功能包括:
- 字符串處理:
StringUtils
、RegExUtils
- 數(shù)字操作:
NumberUtils
- 日期操作:
DateUtils
- 隨機(jī)字符串生成:
RandomStringUtils
- 對(duì)象工具:
ObjectUtils
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot讀取yml文件中的list列表、數(shù)組、map集合和對(duì)象方法實(shí)例
在平時(shí)的yml配置文件中,我們經(jīng)常使用到配置基本數(shù)據(jù)類型的字符串,下面這篇文章主要給大家介紹了關(guān)于springboot讀取yml文件中的list列表、數(shù)組、map集合和對(duì)象的相關(guān)資料,需要的朋友可以參考下2023-02-02Java使用TarsosDSP庫(kù)實(shí)現(xiàn)音頻的處理和格式轉(zhuǎn)換
在音頻處理領(lǐng)域,Java雖然有原生的音頻處理類庫(kù),但其功能相對(duì)基礎(chǔ),而TarsosDSP是一個(gè)強(qiáng)大的開(kāi)源音頻處理庫(kù),提供了豐富的功能,本文將介紹如何在Java中結(jié)合使用TarsosDSP庫(kù),來(lái)實(shí)現(xiàn)音頻的處理和格式轉(zhuǎn)換,需要的朋友可以參考下2025-04-04springboot controller參數(shù)注入方式
這篇文章主要介紹了springboot controller參數(shù)注入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05Java數(shù)組與二維數(shù)組及替換空格實(shí)戰(zhàn)真題講解
數(shù)組對(duì)于每一門編程語(yǔ)言來(lái)說(shuō)都是重要的數(shù)據(jù)結(jié)構(gòu)之一,當(dāng)然不同語(yǔ)言對(duì)數(shù)組的實(shí)現(xiàn)及處理也不盡相同。Java?語(yǔ)言中提供的數(shù)組是用來(lái)存儲(chǔ)固定大小的同類型元素,這篇文章主要介紹了Java數(shù)組與二維數(shù)組及替換空格實(shí)戰(zhàn)真題講解2022-07-07Springboot創(chuàng)建子父工程過(guò)程圖解
這篇文章主要介紹了Springboot創(chuàng)建子父工程過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02配置hadoop環(huán)境mapreduce連接不上hdfs解決
這篇文章主要為大家介紹了配置hadoop環(huán)境mapreduce連接不上hdfs解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10java開(kāi)發(fā)技巧代碼寫的快且bug少的原因分析
這篇文章主要為大家介紹了java開(kāi)發(fā)中代碼寫的快且bug少的原因分析及技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12