Java String join() Method

The String.join() method in Java is used to join multiple strings with a specified delimiter. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. join Method Syntax
  3. Examples
    • Joining Strings with a Delimiter
    • Joining Strings with Different Delimiters
    • Joining an Array of Strings
    • Joining a List of Strings
  4. Conclusion

Introduction

The String.join() method is a static method in the String class introduced in Java 8. It allows you to concatenate multiple strings using a specified delimiter, making it easy to create delimited strings.

join Method Syntax

The syntax for the join method is as follows:

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

And another variation that accepts an Iterable:

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

Examples

Joining Strings with a Delimiter

The join method can be used to join multiple strings with a specified delimiter.

Example

public class JoinExample {
    public static void main(String[] args) {
        String result = String.join(", ", "Alice", "Bob", "Charlie");

        System.out.println("Joined string: " + result);
    }
}

Output:

Joined string: Alice, Bob, Charlie

Joining Strings with Different Delimiters

The join method allows you to use different delimiters to join strings.

Example

public class JoinExample {
    public static void main(String[] args) {
        String result = String.join(" - ", "Java", "Python", "C++");

        System.out.println("Joined string: " + result);
    }
}

Output:

Joined string: Java - Python - C++

Joining an Array of Strings

The join method can be used to join an array of strings with a specified delimiter.

Example

public class JoinExample {
    public static void main(String[] args) {
        String[] languages = {"Java", "Python", "C++"};

        String result = String.join(", ", languages);

        System.out.println("Joined string: " + result);
    }
}

Output:

Joined string: Java, Python, C++

Joining a List of Strings

The join method can also be used to join a list of strings with a specified delimiter.

Example

import java.util.List;

public class JoinExample {
    public static void main(String[] args) {
        List<String> names = List.of("Alice", "Bob", "Charlie");

        String result = String.join("; ", names);

        System.out.println("Joined string: " + result);
    }
}

Output:

Joined string: Alice; Bob; Charlie

Conclusion

The String.join() method in Java provides a convenient way to concatenate multiple strings using a specified delimiter. By understanding how to use this method, you can efficiently create delimited strings in your Java applications. Whether you are joining individual strings, arrays, or lists, the join method offers a powerful and flexible solution for these tasks.

Comments