Java String join() with examples
Last Updated :
08 Apr, 2025
Improve
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. :
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 GfG {
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);
}
}
12
1
// Java program to demonstrate
2
// working of join() method
3
4
class GfG {
5
public static void main(String args[])
6
{
7
// delimiter is "<" and elements are "Four", "Five", "Six", "Seven"
8
String gfg1 = String.join(" < ", "Four", "Five", "Six", "Seven");
9
10
System.out.println(gfg1);
11
}
12
}
Output
Four < Five < Six < Seven
// Java program to demonstrate
// working of join() method
class GfG {
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);
}
}
13
1
// Java program to demonstrate
2
// working of join() method
3
4
class GfG {
5
public static void main(String args[])
6
{
7
// delimiter is " " and elements are "My",
8
// "name", "is", "Niraj", "Pandey"
9
String gfg2 = String.join(" ", "My", "name", "is", "Niraj", "Pandey");
10
11
System.out.println(gfg2);
12
}
13
}
Output
My name is Niraj Pandey
// Java program to demonstrate
// working of join() method
class GfG {
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);
}
}
15
1
// Java program to demonstrate
2
// working of join() method
3
4
class GfG {
5
public static void main(String args[])
6
{
7
// delimiter is "->" and elements are "Wake up",
8
// "Eat", "Play", "Sleep", "Wake up"
9
10
String gfg3 = String.join("-> ", "Wake up", "Eat",
11
"Play", "Sleep", "Wake up");
12
13
System.out.println(gfg3);
14
}
15
}
Output
Wake up-> Eat-> Play-> Sleep-> Wake up