CKTIS / Java
util.StringJoiner: practical reference
util.StringJoiner: practical reference: StringJoiner builds text from elements separated by a delimiter and optional prefix and suffix.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
java.util.StringJoinerStringJoiner builds text from elements separated by a delimiter and optional prefix and suffix.
Use it for manual list formatting or as the mechanism behind Collectors.joining, especially when an empty representation is needed.
The constructor copies delimiter, prefix, and suffix and rejects null for them; setEmptyValue rejects null as well.
Before add, the result is prefix+suffix by default or emptyValue; add(null) adds the text "null", and merge omits the non-empty other joiner’s prefix and suffix.
The example exercises the documented element in a small deterministic program and compares exact standard output.
Examples
public class Main {
public static void main(String[] args) throws Exception {
var joiner = new java.util.StringJoiner(",", "[", "]");
joiner.add("a").add("b");
System.out.println(joiner);
}
}
Result
[a,b]
The example exercises the documented element in a small deterministic program and compares exact standard output.