Java SE API
StringJoiner.add()
Append an element with the configured separator. Java SE API; example uses Java 21-compatible syntax.
Formal Syntax & Structure
java syntaxjava
StringJoiner add(CharSequence newElement)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| newElement | CharSequence | Element text |
Return Value
StringJoiner
Append an element with the configured separator.
Working Example
Example Snippetjava
import java.util.*;import java.util.regex.*;import java.nio.*;import java.nio.charset.*;import java.io.*;import javax.xml.XMLConstants;import javax.xml.parsers.*;import org.xml.sax.*;import org.xml.sax.helpers.DefaultHandler;import org.w3c.dom.*;public class Main { public static void main(String[] args) throws Exception { var joiner = new StringJoiner(" | ", "[", "]"); try (var scanner = new Scanner("12 7 stop").useLocale(Locale.ROOT); var formatter = new Formatter(Locale.ROOT)) { while (scanner.hasNextInt()) joiner.add(Integer.toString(scanner.nextInt())); formatter.format("%s: %04d", scanner.next(), 19); System.out.println(joiner); System.out.println(formatter); } }}