Master Java String Manipulation with strman-java: 23 Essential Methods Explained
This tutorial introduces the strman-java library, a powerful Java 8 string utility with 23 methods—such as append, at, base64Encode, and safeTruncate—providing clear code examples, explanations of underlying Java features, and best‑practice tips for writing cleaner, more efficient code.
Many beginner programmers feel their code looks terrible, but using well‑tested open‑source libraries can dramatically improve code quality. The strman-java library (1.3k stars on GitHub) offers a concise, Java 8‑based API for common string operations.
01 append
Append variable string arguments to the end of a base string. Strman.append("沉", "默", "王", "二"); Result:
沉默王二02 appendArray
Append an array of strings to the end of a base string.
String[] strs = {"默", "王", "二"};
Strman.appendArray("沉", strs);Result:
沉默王二03 at
Retrieve the character at a specified index, supporting negative indices and returning Optional.empty() when out of range.
Strman.at("沉默王二", 0);
Strman.at("沉默王二", -1);
Strman.at("沉默王二", 4);Results: Optional[沉], Optional[二],
Optional.empty04 between
Return an array of substrings located between a start and end delimiter.
String[] results = Strman.between("[沉默王二][一枚有趣的程序员]", "[", "]");
System.out.println(Arrays.toString(results));Result:
[沉默王二, 一枚有趣的程序员]05 chars
Split a string into an array of its individual characters.
String[] results = Strman.chars("沉默王二");
System.out.println(Arrays.toString(results));Result:
[沉, 默, 王, 二]06 charsCount
Count the occurrences of each character in a string.
Map<Character, Long> map = Strman.charsCount("沉默王二的妹妹叫沉默王三");
System.out.println(map);Result:
{的=1, 默=2, 三=1, 妹=2, 沉=2, 叫=1, 王=2, 二=1}07 collapseWhitespace
Replace multiple consecutive whitespace characters with a single space.
Strman.collapseWhitespace("沉默王二 一枚有趣的程序员");Result:
沉默王二 一枚有趣的程序员08 contains
Check whether a string contains a given substring, optionally case‑insensitive.
Strman.contains("沉默王二", "沉");
Strman.contains("Abbc", "a", false);Both calls return true.
09 containsAny
Check whether a string contains any of the strings in an array.
Strman.containsAny("沉默王二", new String[]{"沉", "三"});
Strman.containsAny("沉默王二", new String[]{"沉默", "三"});
Strman.containsAny("沉默王二", new String[]{"不", "三"});Results: true, true, false.
10 endsWith
Verify whether a string ends with a given suffix, with optional case‑sensitivity.
Strman.endsWith("沉默王二", "二");
Strman.endsWith("Abbc", "A", false);Results: true, false.
11 ensureLeft
Ensure a string starts with a given prefix; if not, prepend it.
Strman.ensureLeft("沉默王二", "沉");
Strman.ensureLeft("默王二", "沉");Both calls produce 沉默王二.
12 base64Encode
Encode a string to Base64 using Java 8's Base64 class. Strman.base64Encode("沉默王二"); Result: 5rKJ6buY546L5LqM.
13 binEncode
Convert a string to its 16‑bit Unicode binary representation. Strman.binEncode("沉默王二"); Result:
011011001000100110011110110110000111001110001011010011101000110014 first
Return the first n characters of a string as an Optional.
Strman.first("沉默王二", 0);
Strman.first("沉默王二", 1);
Strman.first("沉默王二", 2);Results: Optional.empty, Optional[沉], Optional[沉默].
15 head
Return the first character of a string (a shortcut for first(value, 1)). Strman.head("沉默王二"); Result: Optional[沉].
16 unequal
Check whether two strings are not equal.
Strman.unequal("沉默王二", "沉默王三");Result: true.
17 insert
Insert a substring at a specified index.
Strman.insert("沉默二", "王", 2);Result: 沉默王二.
18 repeat
Repeat a string a given number of times.
Strman.repeat("沉默王二", 3);Result: 沉默王二沉默王二沉默王二.
19 leftPad
Pad a string on the left to a desired total length.
Strman.leftPad("王二", "沉默", 6);Result: 沉默沉默沉默沉默王二.
20 reverse
Reverse the characters of a string.
Strman.reverse("沉默王二");Result: 二王默沉.
21 safeTruncate
Truncate a string to a maximum length without breaking words, appending a filler.
Strman.safeTruncate("Java is the best", 13, "...");Result: Java is....
22 shuffle
Randomly shuffle the characters of a string.
Strman.shuffle("沉默王二");Result (example): 王默二沉.
The library also provides many other useful utilities such as surround, rightPad, removeEmptyStrings, and more; see the official documentation for the full list.
https://github.com/shekhargulati/strman-java/wiki
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
