Java .join()

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.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours

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

Contribute to Docs

Learn Java on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours