.join()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Mar 5, 2023
Contribute to Docs

The .join() method returns a new string composed of the elements joined together with the specified delimiter.

Syntax

String.join(delimiter, element1, element2...elementN);
  • delimiter (required): The delimiter that is used to join the elements.
  • elements(required): The elements to be joined together.

Example

The example below uses .join() to concatenate several names with a pipe delimiter:

// JoinPipe.java
class JoinPipe {
public static void main(String[] args) {
String names = String.join(" | ", "Carl", "Jonas", "Emily", "Daniel", "Nina");
System.out.println(names);
}
}

The output would be:

Carl | Jonas | Emily | Daniel | Nina

All contributors

Looking to contribute?

Learn Java on Codecademy