Java String join() Method with Examples

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

In this article, we will delve into the remarkable capabilities of the join() method within the Java String class. Introduced in Java 1.8, this method proves to be an invaluable asset for efficient string manipulation. It simplifies the process of concatenating multiple strings using a specific delimiter, offering a clean and organized approach to join string elements.

With two distinct variants designed to handle both arrays and Iterable collections of CharSequence elements and the freedom to select any delimiter, the join() method empowers Java programmers to streamline their string concatenation tasks.

To gain a deeper understanding of its utility, we will explore practical examples that showcase its adaptability in real-world scenarios. Whether you’re dealing with arrays, lists, or any other collection of strings, the join() method is poised to become an indispensable resource in your Java programming toolkit.

Types of join() Methods in Java

There are two variants of join() method in the Java String class:

Method 1:

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

This join() method takes a delimiter and an array of CharSequence elements to be joined.

Method 2:

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

This join() method takes a delimiter and an Iterable collection of CharSequence elements to be joined. The delimiter can be any string like comma, space, hyphen etc. The elements are the string components to be joined with the delimiter.

The join() method returns a concatenated string with the delimiter inserted between each element.

Example Demonstrations

Let’s look at some examples to understand the usage of String join() method in Java:

Example 1:

public class JoinExample1 {

  public static void main(String[] args) {

    String[] elements = {"Apple", "Banana", "Cherry", "Date"};

    String result = String.join("|", elements);

    System.out.println(result);

  }

}

Output:

Apple|Banana|Cherry|Date

Example 2:

import java.util.ArrayList;

public class JoinExample2 {

  public static void main(String[] args) {

    ArrayList<String> colors = new ArrayList<>();
    colors.add("Red");
    colors.add("Green");
    colors.add("Blue");
    colors.add("Yellow");
    colors.add("Purple");

    String result = String.join(" - ", colors);

    System.out.println(result);

  }

}

Output:
Red – Green – Blue – Yellow – Purple

Example 3:

public class JoinExample3 {

  public static void main(String[] args) {

    String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

    String result = String.join(" => ", days);

    System.out.println(result);

  }

}

Output:
Monday => Tuesday => Wednesday => Thursday => Friday

Conclusion

In conclusion, the Java String class’s join() method offers a convenient and efficient way to concatenate strings with specific delimiters, simplifying the process of combining elements such as words, phrases, or values. With two distinct variants, it accommodates both arrays and Iterable collections of CharSequence elements, and the choice of delimiter is entirely flexible.

The examples provided illustrate its versatility and practicality in real-world scenarios. Whether you are working with data structures or any other collection of strings, the join() method proves to be an invaluable asset for string manipulation in Java.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

courses

Leave a Reply

Your email address will not be published. Required fields are marked *