EN
Java - string join() method example
0 points
In this article, we would like to show you string join()
method example in Java.
Quick solution:
xxxxxxxxxx
1
String joinString = String.join(" ", "Dirask", "is", "awesome", "!");
2
System.out.println(joinString);
Syntax | public 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. |
Result | a new String that is composed of the elements separated by the delimiter |
Description | The method returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter. |
Java string join()
method returns a string joined with a given delimiter which is copied for each element.
In this example, we join string elements with an empty string as a delimiter.
xxxxxxxxxx
1
public class Example {
2
3
public static void main(String[] args) {
4
String joinString = String.join(" ", "Dirask", "is", "awesome", "!");
5
System.out.println(joinString);
6
}
7
}
Output:
xxxxxxxxxx
1
Dirask is awesome !
In this example, we use join()
method with - as a delimiter to display string elements as a date.
xxxxxxxxxx
1
public class Example {
2
3
public static void main(String[] args) {
4
String joinString = String.join("-", "2021", "03", "11");
5
System.out.println(joinString);
6
}
7
}
Output:
xxxxxxxxxx
1
2021-03-11