首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Apache Commons Lang StringUtils 核心方法詳解與實戰範例

     編輯:Java 相關知識

前言

org.apache.commons.lang3.StringUtils 是 Java 開發中最常用的字串處理工具類,提供 130 多個 static 方法,且皆為 null-safe(傳入 null 不拋出 NPE)。本文精選高頻方法分類詳解,附帶實測輸出對照表。

常用方法分類速查表

分類代表方法用途
空值檢查isEmpty, isNotEmpty, isBlank, isNotBlank判斷字串是否為 null、空字串、或僅含空白字元
去除空白trim, trimToNull, trimToEmpty, strip, stripToNull, stripToEmpty移除首尾空白字元(strip 含 Unicode 空白)
比較equals, equalsIgnoreCasenull-safe 字串比較
查找索引indexOf, lastIndexOf, ordinalIndexOf尋找字元/子字串位置,支援指定起始位置與第 N 次出現
子字串substring, substringBefore, substringAfter, substringBetween擷取子字串
分割/連接split, join字串分割與陣列/集合連接
替換/移除replace, replaceOnce, remove, removeStart, removeEnd字串替換與移除
大小寫轉換capitalize, uncapitalize, swapCase, lowerCase, upperCase字串大小寫轉換
填充/對齊leftPad, rightPad, center, repeat字串填充、置中、重複

核心方法實測對照

1. 空值檢查:isEmpty vs isBlank

// isEmpty:僅判斷 null 或 ""
StringUtils.isEmpty(null)      // true
StringUtils.isEmpty("")        // true
StringUtils.isEmpty(" ")       // false(含空格不視為 empty)
StringUtils.isEmpty("\t\n")    // false

// isBlank:判斷 null、""、或僅含空白字元(含 \t\n\r\f)
StringUtils.isBlank(null)      // true
StringUtils.isBlank("")        // true
StringUtils.isBlank(" ")       // true
StringUtils.isBlank("\t\n")    // true
StringUtils.isBlank("\b")      // false(退位符非空白字元)

2. 去除空白:trim vs strip 系列

StringUtils.trim(null)         // null
StringUtils.trim("")           // ""
StringUtils.trim("  ")         // ""
StringUtils.trim("\t\n")       // ""
StringUtils.trim("\b")         // ""(trim 視 \b 為控制字元而移除)

StringUtils.strip(null)        // null
StringUtils.strip("  ")        // ""
StringUtils.strip("\t\n")      // ""
StringUtils.strip("\b")        // "\b"(strip 僅移除 Character.isWhitespace 為 true 的字元,\b 不在內)

3. 變體:toNull / toEmpty

// trimToNull:trim 後若為空字串則回傳 null
StringUtils.trimToNull("  ")   // null
StringUtils.trimToNull("abc")  // "abc"

// trimToEmpty:trim 後若為 null 則回傳 ""
StringUtils.trimToEmpty(null)  // ""
StringUtils.trimToEmpty("  ")  // ""

4. 比較:equals / equalsIgnoreCase

StringUtils.equals(null, null)           // true
StringUtils.equals("abc", "ABC")         // false
StringUtils.equalsIgnoreCase("abc", "ABC") // true

5. 索引查找:indexOf / ordinalIndexOf

StringUtils.indexOf("aabaabaa", "a", 0)     // 0
StringUtils.indexOf("aabaabaa", "a", 1)     // 1
StringUtils.ordinalIndexOf("aabaabaa", "a", 3) // 3(第 3 次出現位置)
StringUtils.ordinalIndexOf("aabaabaa", "ab", 2) // 4

實務最佳實踐

  • 依賴管理:Maven 引入 org.apache.commons:commons-lang3(注意 groupId 為 commons-lang3,非舊版 commons-lang)。
  • 避免重複造輪子:專案中統一使用 StringUtils,減少自訂工具類與 NPE 風險。
  • 效能考量:熱點路徑頻繁呼叫時,簡單操作(如 str == null || str.isEmpty())可能比工具類略快,但可讀性與安全性通常優先。
  • 搭配 Java 11+:String.isBlank()、String.strip() 等原生方法已涵蓋部分功能,新專案可評估原生 API 是否足夠。

總結

StringUtils 是 Java 開發者的「瑞士刀」,熟練掌握其核心方法能大幅提升字串處理的效率與健壯性。建議將常用方法整理成團隊速查表,程式碼審查時優先檢查是否妥善使用 null-safe 方法。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved