Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java String join() method
Syntax and Examples



Introduction to String.join()

The String.join() method in Java is a convenient way to concatenate strings with a specified delimiter. It simplifies what used to require more verbose code, making your string manipulation cleaner and easier to read. Think of it like gluing pieces of string together using a specific connector – that connector is the delimiter.

Syntax

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

Parameters

ParameterDescription
delimiterThe sequence of characters to insert between the elements. Can be a String or any other CharSequence.
elements (variable)An array of CharSequence objects to join together. Or, an iterable collection of CharSequence objects.

Return Value

The join() method returns a single string that is the result of concatenating all the elements separated by the specified delimiter.

Examples

Example 1: Joining Strings with an Array

This example demonstrates how to use String.join() to combine an array of strings into a single string, using a comma and space as the delimiter.

import java.util.*;

public class JoinExample {
    public static void main(String[] args) {
        String[] words = {"Hello", "World", "Java"};
        String joinedString = String.join(", ", words);
        System.out.println(joinedString);
    }
}
Hello, World, Java

Explanation: We have an array of strings called words. We call String.join() with ", " as the delimiter and pass the words array as the elements to be joined. The result is a new string where each word from the array is separated by a comma and a space.

Example 2: Joining Strings with an Iterable

This example shows how to use String.join() with an iterable collection, like an ArrayList.

import java.util.*;

public class JoinIterableExample {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");

        String joinedNames = String.join(" - ", names);
        System.out.println(joinedNames);
    }
}
Alice - Bob - Charlie

Explanation: Here, we create an ArrayList of strings called names. We then use String.join() to join these names together using " - " as the delimiter. The resulting string combines all the names with a hyphen and space in between.

Example 3: Using an Empty Delimiter

This example demonstrates what happens when you use an empty string as the delimiter, effectively concatenating the strings directly without any separator.

import java.util.*;

public class JoinEmptyDelimiterExample {
    public static void main(String[] args) {
        String[] parts = {"a", "b", "c"};
        String concatenatedString = String.join("", parts);
        System.out.println(concatenatedString);
    }
}
abc

Explanation: In this case, we pass an empty string ("") as the delimiter to String.join(). This means there will be no separator between the elements when they are joined together, resulting in a single concatenated string.

Example 4: Joining with Null Elements

This example shows how String.join() handles null elements within the array or iterable.

import java.util.*;

public class JoinNullElementsExample {
    public static void main(String[] args) {
        String[] mixed = {"one", null, "three"};
        String joinedWithNulls = String.join(", ", mixed);
        System.out.println(joinedWithNulls);
    }
}
one, , three

Explanation: When String.join() encounters a null element, it treats it as an empty string. So, in this example, the null element effectively disappears and is replaced by the delimiter.

Example 5: Joining with Special Characters

This demonstrates using special characters within the delimiter.

import java.util.*;

public class JoinSpecialCharsExample {
    public static void main(String[] args) {
        String[] words = {"first", "second", "third"};
        String joinedWithNewline = String.join("\n", words);
        System.out.println(joinedWithNewline);
    }
}
first
second
third

Explanation: This example demonstrates joining with a newline character represented by "\n". The output shows each word on a separate line due to the newline delimiter.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M