Java : StringJoiner with Examples

StringJoiner (Java SE 22 & JDK 22) with Examples.
You will find code examples on most StringJoiner methods.


Summary

StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.

Class diagram

final var sj = new StringJoiner("-", "[", "]");

sj.add("aaa").add("bbb").add("ccc");

final var ret = sj.toString();
System.out.println(ret); // "[aaa-bbb-ccc]"
final var sj = new StringJoiner(", ", "(", ")");

sj.add("xxx").add("yyy").add("zzz");

final var ret = sj.toString();
System.out.println(ret); // "(xxx, yyy, zzz)"

Similar APIs.

final var list = List.of("aaa", "bbb", "ccc");

final var ret = String.join("-", list);
System.out.println(ret); // "aaa-bbb-ccc"
final var list = List.of("aaa", "bbb", "ccc");

final var ret = list.stream().collect(Collectors.joining("-", "[", "]"));
System.out.println(ret); // "[aaa-bbb-ccc]"

Constructors

StringJoiner (CharSequence delimiter)

Constructs a StringJoiner with no characters in it, with no prefix or suffix, and a copy of the supplied delimiter.

final var sj = new StringJoiner("-");

sj.add("aaa").add("bbb").add("ccc");
System.out.println(sj); // "aaa-bbb-ccc"

StringJoiner (CharSequence delimiter, CharSequence prefix, CharSequence suffix)

Constructs a StringJoiner with no characters in it using copies of the supplied prefix, delimiter and suffix.

final var sj = new StringJoiner("-", "[", "]");

sj.add("aaa").add("bbb").add("ccc");
System.out.println(sj); // "[aaa-bbb-ccc]"

Methods

StringJoiner add (CharSequence newElement)

Adds a copy of the given CharSequence value as the next element of the StringJoiner value.

final var sj = new StringJoiner(", ");

sj.add("aaa").add("bbb").add("ccc").add(null);
System.out.println(sj); // "aaa, bbb, ccc, null"

int length ()

Returns the length of the String representation of this StringJoiner.

final var sj = new StringJoiner("-", "[", "]");

System.out.println(sj); // "[]"
System.out.println(sj.length()); // 2

sj.add("aaa");
System.out.println(sj); // "[aaa]"
System.out.println(sj.length()); // 5

sj.add("bbb");
System.out.println(sj); // "[aaa-bbb]"
System.out.println(sj.length()); // 9

StringJoiner merge (StringJoiner other)

Adds the contents of the given StringJoiner without prefix and suffix as the next element if it is non-empty.

final var sj1 = new StringJoiner("-", "[", "]");
final var sj2 = new StringJoiner(":");

sj1.add("aaa").add("bbb").add("ccc");
System.out.println(sj1); // "[aaa-bbb-ccc]"

sj2.add("012").add("345").add("678");
System.out.println(sj2); // "012:345:678"

sj1.merge(sj2).add("eee");
System.out.println(sj1); // "[aaa-bbb-ccc-012:345:678-eee]"

StringJoiner setEmptyValue (CharSequence emptyValue)

Sets the sequence of characters to be used when determining the string representation of this StringJoiner and no elements have been added yet, that is, when it is empty.

final var sj = new StringJoiner("-", "[", "]");
System.out.println(sj); // "[]"

sj.setEmptyValue("empty");
System.out.println(sj); // "empty"

sj.add("aaa").add("bbb").add("ccc");

System.out.println(sj); // "[aaa-bbb-ccc]"

String toString ()

Returns the current value, consisting of the prefix, the values added so far separated by the delimiter, and the suffix, unless no elements have been added in which case, the prefix + suffix or the emptyValue characters are returned.

final var sj = new StringJoiner("-", "[", "]");

sj.add("aaa").add("bbb").add("ccc");

final var ret = sj.toString();
System.out.println(ret); // "[aaa-bbb-ccc]"

Related posts

To top of page