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.

JavaCli Tooling

Code

snippet
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

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:`.