Java實用工具庫commons-lang3的使用
Java實用工具庫commons-lang3
Apache Commons Lang 是一個流行的 Java 實用工具庫,其中 commons-lang3 是其最新的主流版本,用于增強 Java 核心功能,特別是對 java.lang 包的擴展。
以下是 commons-lang3 的功能概述,以及如何使用其中的一些工具(如 RegExUtils)。
Maven 依賴
在使用 commons-lang3 之前,需要在項目中添加 Maven 依賴:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version> <!-- 或最新版本 -->
</dependency>主要工具與功能
1. StringUtils
- 提供對字符串操作的各種實用方法。
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
- 用于處理正則表達式的高級工具。
import org.apache.commons.lang3.RegExUtils;
public class RegExUtilsExample {
public static void main(String[] args) {
String text = "key1:value1, key2:value2, key3:value3";
// 替換冒號為下劃線
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
- 提供日期和時間操作的工具。
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
- 生成隨機字符串的工具。
import org.apache.commons.lang3.RandomStringUtils;
public class RandomStringUtilsExample {
public static void main(String[] args) {
// 生成隨機字母字符串
String randomAlphabetic = RandomStringUtils.randomAlphabetic(10);
System.out.println(randomAlphabetic);
// 生成隨機數(shù)字字符串
String randomNumeric = RandomStringUtils.randomNumeric(10);
System.out.println(randomNumeric);
}
}6. ObjectUtils
- 提供對對象操作的工具。
import org.apache.commons.lang3.ObjectUtils;
public class ObjectUtilsExample {
public static void main(String[] args) {
String str = null;
// 如果對象為 null,提供默認值
System.out.println(ObjectUtils.defaultIfNull(str, "Default Value")); // "Default Value"
// 判斷多個對象是否都不為空
System.out.println(ObjectUtils.allNotNull("A", "B", "C")); // true
System.out.println(ObjectUtils.allNotNull("A", null, "C")); // false
}
}總結(jié)
commons-lang3 提供了一系列增強 Java 核心功能的工具類,常用功能包括:
- 字符串處理:
StringUtils、RegExUtils - 數(shù)字操作:
NumberUtils - 日期操作:
DateUtils - 隨機字符串生成:
RandomStringUtils - 對象工具:
ObjectUtils
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java結(jié)合OpenCV實現(xiàn)圖形模板匹配實戰(zhàn)教程
OpenCV是一個廣泛應用于計算機視覺任務的開源庫,支持多種編程語言,其中包括Java,本文給大家介紹Java結(jié)合OpenCV實現(xiàn)圖形模板匹配實戰(zhàn)教程,感興趣的朋友跟隨小編一起看看吧2025-11-11
java數(shù)據(jù)結(jié)構(gòu)與算法之noDups去除重復項算法示例
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之noDups去除重復項算法實現(xiàn)技巧,程序代碼非常簡單,關鍵在于循環(huán)與判定,需要的朋友可以參考下2016-08-08
SpringBoot整合Dubbo框架,實現(xiàn)RPC服務遠程調(diào)用
Dubbo是一款高性能、輕量級的開源Java RPC框架,它提供了三大核心能力:面向接口的遠程方法調(diào)用,智能容錯和負載均衡,以及服務自動注冊和發(fā)現(xiàn)。今天就來看下SpringBoot整合Dubbo框架的步驟2021-06-06

