Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

String.join — joining text with a delimiter

String.join builds text from elements separated by a delimiter. It accepts an Iterable or varargs arguments.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
public static String join(CharSequence delimiter, CharSequence... elements)

String.join is a static method returning a new String. It inserts delimiter between elements, not before or after them.

The Iterable<? extends CharSequence> overload accepts, for example, a List<String> and follows iteration order. The CharSequence... variant accepts separate arguments or an array. Elements must be CharSequence values, not arbitrary objects.

A null element produces the text "null". However, null as the delimiter or the entire elements argument causes NullPointerException.

Examples

import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> parts = List.of("red", "green", "blue");
        System.out.println(String.join(" | ", parts));
        System.out.println(String.join("-", "A", null, "B"));
    }
}

Result

red | green | blue
A-null-B

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources