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:
String joinString = String.join(" ", "Dirask", "is", "awesome", "!");
System.out.println(joinString);
1. Documentation
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. |
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