在日常 Java 開發中,字符串操作無處不在:拼接、截取、查找、替換、大小寫轉換、編碼解碼……雖然 JDK 自帶的 String、StringBuilder 和 Apache Commons Lang 的 StringUtils 已經很好用,但面對複雜場景(如「安全截斷不斷詞」、「Base64/十六進制/二進制互轉」、「駝峰/下劃線/短橫線命名風格互轉」)時,代碼往往顯得冗長且易出錯。
strman-java 正是為了解決這些痛點而生的輕量級字符串處理庫,它封裝了 100+ 實用方法,語義清晰、鏈式調用友好,能讓字符串處理代碼更簡潔、可讀性更強。
<dependency>
<groupId>com.shekhargulati</groupId>
<artifactId>strman</artifactId>
<version>0.2.0</version>
</dependency>
implementation 'com.shekhargulati:strman:0.2.0'
下面通過一個完整的測試類,展開講解 strman-java 最常用的幾大類 API。所有示例均基於 strman.Strman 靜態方法調用。
// 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"
// 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..."
// 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
// 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"]
掌握 strman-java,能讓你在處理字符串時「少寫代碼、少踩坑、易維護」,是 Java 開發工具箱裡值得常備的利器。