Languages
[Edit]
EN

Java - string join() method example

0 points
Created by:
Palusbug
515

In this article, we would like to show you string join() method example in Java.

Quick solution:

String joinString = String.join(" ", "Dirask", "is", "awesome", "!");
System.out.println(joinString);

 

1. Documentation

Syntaxpublic static String join(CharSequence delimiter, CharSequence... elements)
Parameters

delimiter - a sequence of characters that is used to separate each of the elements,

elements - the elements to join together.

Resulta new String that is composed of the elements separated by the delimiter
DescriptionThe method returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.

2. Practical examples

Java string join() method returns a string joined with a given delimiter which is copied for each element.

Example 1

In this example, we join string elements with an empty string as a delimiter.

public class Example {

    public static void main(String[] args) {
        String joinString = String.join(" ", "Dirask", "is", "awesome", "!");
        System.out.println(joinString);
    }
}

Output:

Dirask is awesome !

Example 2

In this example, we use join() method with - as a delimiter to display string elements as a date.

public class Example {

    public static void main(String[] args) {
        String joinString = String.join("-", "2021", "03", "11");
        System.out.println(joinString);
    }
}

Output:

2021-03-11
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - String documentation

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join