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" // 反轉字符串 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
- 提供數字操作的工具方法。
import org.apache.commons.lang3.math.NumberUtils; public class NumberUtilsExample { public static void main(String[] args) { // 判斷是否是數字 System.out.println(NumberUtils.isCreatable("123")); // true System.out.println(NumberUtils.isCreatable("12.3")); // true System.out.println(NumberUtils.isCreatable("abc")); // false // 找到數組中的最大值 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); // 添加天數 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); // 生成隨機數字字符串 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 } }
總結
commons-lang3
提供了一系列增強 Java 核心功能的工具類,常用功能包括:
- 字符串處理:
StringUtils
、RegExUtils
- 數字操作:
NumberUtils
- 日期操作:
DateUtils
- 隨機字符串生成:
RandomStringUtils
- 對象工具:
ObjectUtils
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
springboot讀取yml文件中的list列表、數組、map集合和對象方法實例
在平時的yml配置文件中,我們經常使用到配置基本數據類型的字符串,下面這篇文章主要給大家介紹了關于springboot讀取yml文件中的list列表、數組、map集合和對象的相關資料,需要的朋友可以參考下2023-02-02Java使用TarsosDSP庫實現(xiàn)音頻的處理和格式轉換
在音頻處理領域,Java雖然有原生的音頻處理類庫,但其功能相對基礎,而TarsosDSP是一個強大的開源音頻處理庫,提供了豐富的功能,本文將介紹如何在Java中結合使用TarsosDSP庫,來實現(xiàn)音頻的處理和格式轉換,需要的朋友可以參考下2025-04-04配置hadoop環(huán)境mapreduce連接不上hdfs解決
這篇文章主要為大家介紹了配置hadoop環(huán)境mapreduce連接不上hdfs解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10