The java.lang.string.join() method concatenates the given elements with the delimiter and returns the concatenated string.Note that if an element is null, then null is added.The join() method is included in java string since JDK 1.8.
There are two types of join() methods in java string.
Syntax:
public static String join(CharSequence deli, CharSequence... ele) and public static String join (CharSequence deli, Iterable<? extends CharSequence> ele) Parameters: deli- delimiter to be attached with each element ele- string or char to be attached with delimiter Returns : string joined with delimiter.
// Java program to demonstrate // working of join() method class Gfg1 { public static void main(String args[]) { // delimiter is "<" and elements are "Four", "Five", "Six", "Seven" String gfg1 = String.join(" < ", "Four", "Five", "Six", "Seven"); System.out.println(gfg1); } } |
chevron_right
filter_none
Output:
Four < Five < Six < Seven
// Java program to demonstrate // working of join() method class Gfg2 { public static void main(String args[]) { // delimiter is " " and elements are "My", // "name", "is", "Niraj", "Pandey" String gfg2 = String.join(" ", "My", "name", "is", "Niraj", "Pandey"); System.out.println(gfg2); } } |
chevron_right
filter_none
Output:
My name is Niraj Pandey
// Java program to demonstrate // working of join() method class Gfg3 { public static void main(String args[]) { // delimiter is "->" and elements are "Wake up", // "Eat", "Play", "Sleep", "Wake up" String gfg3 = String.join("-> ", "Wake up", "Eat", "Play", "Sleep", "Wake up"); System.out.println(gfg3); } } |
chevron_right
filter_none
Output:
Wake up-> Eat-> Play-> Sleep-> Wake up
Recommended Posts:
- Join two ArrayLists in Java
- Ints join() function | Guava | Java
- Comparison of yield(), join() and sleep() in Java
- Differences between wait() and join() methods in Java
- Java Concurrency - yield(), sleep() and join() methods
- isAlive() and join() methods of Thread Class in Java
- Java String getChars() with examples
- Java String toLowerCase() with examples
- Java String format() with examples
- Java String endsWith() with examples
- Java String concat() with examples
- Java String subSequence() method with Examples
- String toString() Method in java with Examples
- Split() String method in Java with examples
- ZoneOffset of(String) method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



