Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

String.join()

Joins strings with a separator.

Available since: Java 8

Syntax

static String join(CharSequence delimiter, CharSequence... elements)

Parameters

ParameterTypeDescription
delimiterCharSequenceThe string inserted between elements.
elementsCharSequence... / IterableStrings separated by commas, an array or a collection of strings (for example a list).

Return value

StringOne string made of all the elements.

Examples

From separate strings and from a list:

void main() {
    IO.println(String.join("-", "2025", "03", "14"));
 
    List<String> names = List.of("Anna", "Oleg", "Maria");
    IO.println(String.join(", ", names));
}

Output:

2025-03-14
Anna, Oleg, Maria

Notes

The separator goes only between elements, not at the end. For lists of numbers use a stream: numbers.stream().map(String::valueOf).collect(Collectors.joining(", ")).

See also

  • String.split() — Splits a string into an array of parts by a separator.
  • StringBuilder — A mutable string: useful when text is built piece by piece, especially in a loop.
  • String.repeat() — Repeats a string the given number of times.
  • Collectors.joining() — A collector that joins the strings of a stream into one.

Related recipes

In the tutorial

  • String methods — The main String methods in Java: searching, substring, changing case, strip, replace, split, join, repeat and string checks.

Thanks for reading!

Donate to the site — we work hard for you.