String Join And Split Processing
Round-trip delimited data with String.join, split, and StringJoiner so serialization stays consistent on both ends.
Code
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
public class DelimitedRoundtrip {
public static void main(String[] args) {
List<String> fields = List.of("alice", "admin", "active");
// join:集合一行序列化
String line = String.join("|", fields);
System.out.println("encoded: " + line);
// split:反向解码;转义正则元字符的管道要 \\|
String[] decoded = line.split("\\|");
System.out.println("decoded count = " + decoded.length);
// StringJoiner:带前后缀的 CSV 风格输出
StringJoiner csv = new StringJoiner(", ", "[", "]");
csv.setEmptyValue("(none)");
csv.add("a").add("b");
System.out.println(csv);
System.out.println(new StringJoiner(", ", "[", "]").setEmptyValue("(none)"));
}
}How it works
String Join And Split Processing reduces to the class DelimitedRoundtrip and the function main.
Putting it to use
Running the file prints labeled results such as encoded:, decoded count = — each operation’s effect is visible in the output.
Core lines
List<String> fields = List.of("alice", "admin", "active");
StringJoiner csv = new StringJoiner(", ", "[", "]");
String line = String.join("|", fields);
Interactive Tools for This Topic
FAQ
- What does String Join And Split Processing print when run?
- Execute the whole file; each printed line is prefixed with a label like `encoded:`, `decoded count =`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.