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

Java 8 字符串處理神器:strman-java 實戰指南與核心 API 詳解

     編輯:Java 相關知識

前言:為什麼需要 strman-java?

在日常 Java 開發中,字符串操作無處不在:拼接、截取、查找、替換、大小寫轉換、編碼解碼……雖然 JDK 自帶的 String、StringBuilder 和 Apache Commons Lang 的 StringUtils 已經很好用,但面對複雜場景(如「安全截斷不斷詞」、「Base64/十六進制/二進制互轉」、「駝峰/下劃線/短橫線命名風格互轉」)時,代碼往往顯得冗長且易出錯。

strman-java 正是為了解決這些痛點而生的輕量級字符串處理庫,它封裝了 100+ 實用方法,語義清晰、鏈式調用友好,能讓字符串處理代碼更簡潔、可讀性更強。

快速接入:Maven / Gradle 依賴配置

Maven

<dependency>
    <groupId>com.shekhargulati</groupId>
    <artifactId>strman</artifactId>
    <version>0.2.0</version>
</dependency>

Gradle

implementation 'com.shekhargulati:strman:0.2.0'

核心功能實戰演示

下面通過一個完整的測試類,展開講解 strman-java 最常用的幾大類 API。所有示例均基於 strman.Strman 靜態方法調用。

1. 字符串拼接與前綴/後綴控制

// append:尾部追加多個字符/字符串
Strman.append("f", "o", "o", "b", "a", "r"); // => "foobar"

// prepend:頭部追加
Strman.prepend("r", "f", "o", "o", "b", "a"); // => "foobar"

// appendArray:尾部追加字符串數組
Strman.appendArray("f", new String[]{"o","o","b","a","r"}); // => "foobar"

// ensureLeft / ensureRight:確保以指定前綴/後綴開頭/結尾,否則自動補全
Strman.ensureLeft("bar", "foo");   // => "foobar"
Strman.ensureRight("foo", "bar");  // => "foobar"

2. 字符查找、截取與子串操作

// at:按索引取字符(支持負數逆向,越界返回 Optional.empty)
Strman.at("foobar", 3).orElse(""); // => "b"

// between:提取兩個分隔符之間的所有子串
Strman.between("[abc], [def]", "[", "]"); // => ["abc", "def"]

// first / last:從頭/尾取指定長度
Strman.first("foobar", 3); // => "foo"
Strman.last("foobar", 3);  // => "bar"

// head / tail:取首字符 / 去首字符
Strman.head("foobar"); // => "f"
Strman.tail("foobar"); // => "oobar"

// removeLeft / removeRight:移除前綴/後綴(若存在)
Strman.removeLeft("foobar", "foo");  // => "bar"
Strman.removeRight("foobar", "bar"); // => "foo"

// safeTruncate / truncate:安全截斷(不斷詞) vs 普通截斷
Strman.safeTruncate("A Javascript string manipulation library.", 19, "..."); 
// => "A Javascript..."
Strman.truncate("A Javascript string manipulation library.", 19, "..."); 
// => "A Javascript str..."

3. 判斷與統計類方法

// contains / containsAll / containsAny:包含判斷(可忽略大小寫)
Strman.contains("foo bar", "FOO", false);       // true
Strman.containsAll("foo bar", new String[]{"FOO","bar"}, false); // true
Strman.containsAny("foo bar", new String[]{"FOO","BAR","Test"}, false); // true

// countSubstr:子串出現次數(可選是否重疊、是否忽略大小寫)
Strman.countSubstr("aaaAAAaaa", "aaa");           // 2(不重疊、區分大小寫)
Strman.countSubstr("aaaAAAaaa", "aaa", false, false); // 3(不重疊、忽略大小寫)

// endsWith / startsWith:結尾/開頭判斷
Strman.endsWith("foo bar", "BAR", false); // true

// inequal:判斷不相等
Strman.inequal("a", "b"); // true

4. 編碼/解碼與格式轉換

// Base64
Strman.base64Encode("strman"); // => "c3RybWFu"

// 二進制(16位)
Strman.binEncode("A");   // => "0000000001000001"
Strman.binDecode("0000000001000001"); // => "A"

// 十進制(5位)
Strman.decEncode("A");   // => "00065"
Strman.decDecode("00065"); // => "A"

// 十六進制(4位)
Strman.hexEncode("A");   // => "0041"
Strman.hexDecode("0041"); // => "A"

// HTML 實體轉義
Strman.htmlEncode("Ш");  // => "Ш"
Strman.htmlDecode("Ш"); // => "Ш"

// 命名風格互轉
Strman.toCamelCase("hello world");      // => "helloWorld"
Strman.toStudlyCase("hello world");     // => "HelloWorld"
Strman.toSnakeCase("hello world");      // => "hello_world"
Strman.toKebabCase("hello world");      // => "hello-world"
Strman.toDecamelize("helloWorld", "-"); // => "hello-world"

// 其他實用工具
Strman.slugify("foo bar");           // => "foo-bar"(URL 友好)
Strman.transliterate("fóõ bár"); // => "foo bar"(去除讀音符號)
Strman.surround("div", "<", ">");   // => "<div>"(包裹字符串)
Strman.shuffle("shekhar");           // => 隨機打亂字符順序
Strman.reverse("foobar");            // => "raboof"
Strman.repeat("1", 3);               // => "111"
Strman.collapseWhitespace("foo   bar"); // => "foo bar"(多空格合併)
Strman.removeSpaces(" str man ");    // => "strman"(移除所有空格)
Strman.removeNonWords("foo&bar-");   // => "foobar"(只保留字母數字下劃線)
Strman.leftPad("1", "0", 5);         // => "00001"
Strman.rightPad("1", "0", 5);        // => "10000"
Strman.leftTrim(" strman ");         // => "strman "
Strman.rightTrim(" strman ");        // => " strman"
Strman.removeEmptyStrings(new String[]{"aa",""," ","bb",null}); // => ["aa","bb"]

總結與建議

  • 優勢:API 覆蓋面極廣、方法命名語義化、無第三方依賴、Jar 包極小(< 50 KB)。
  • 適用場景:需要大量非標準字符串處理的業務代碼、工具類封裝、老項目遷移替換繁瑣的手寫工具方法。
  • 注意:庫已多年未更新(最後版本 0.2.0 發布於 2016 年),若項目對安全合規要求極高,建議審計源碼或考慮 Apache Commons Text / Guava 作為替代。

掌握 strman-java,能讓你在處理字符串時「少寫代碼、少踩坑、易維護」,是 Java 開發工具箱裡值得常備的利器。

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