StringJoiner Delimited Output
Assemble delimited output with StringJoiner: prefix and suffix framing, an explicit empty value, and merge that reuses the first joiner's delimiter.
Code
import java.util.StringJoiner;
public class CsvRowBuilder {
public static void main(String[] args) {
// delimited output:delimiter 分隔元素,前后缀框住整行
StringJoiner row = new StringJoiner(",", "[", "]");
row.add("id=7").add("status=PAID").add("total=42.00");
System.out.println(row);
// setEmptyValue:空 joiner 不输出裸前后缀,而是显式占位
StringJoiner empty = new StringJoiner(",", "[", "]").setEmptyValue("(none)");
System.out.println("empty: " + empty);
// merge:沿用第一个 joiner 的分隔符与前缀后缀
StringJoiner flags = new StringJoiner(";").add("--verbose").add("--dry-run");
System.out.println("merged: " + row.merge(flags));
System.out.println("length: " + row.length());
}
}Inside the snippet
Count the class CsvRowBuilder and the function main as the entire surface area of StringJoiner Delimited Output.
Putting it to use
The demo output is labeled (empty:, merged:, length:), so every printed line maps back to its originating operation.
Core lines
StringJoiner empty = new StringJoiner(",", "[", "]").setEmptyValue("(none)");
StringJoiner flags = new StringJoiner(";").add("--verbose").add("--dry-run");
row.add("id=7").add("status=PAID").add("total=42.00");
Interactive Tools for This Topic
CSV Column AnalyzerProfile every CSV column with inferred types, empty-cell counts, unique values, and numeric min and max ranges.CSV to JSON ConverterConvert comma-separated values into a JSON array of objects using the first row as field names, with support for quoted and escaped fields.CSV to Markdown TableConvert CSV data, including quoted fields with embedded commas and newlines, into a clean GitHub-flavored Markdown table with selectable alignment.CSV to TSV ConverterParse RFC 4180 CSV with quoted fields and re-serialize using a tab, pipe, semicolon, or custom delimiter, with optional quoting.
FAQ
- What does StringJoiner Delimited Output print when run?
- Run it as a complete program — the output lines carry labels such as `empty:`, `merged:`, `length:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.