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.
class Gfg1 {
public static void main(String args[])
{
String gfg1 = String.join( " < " , "Four" , "Five" , "Six" , "Seven" );
System.out.println(gfg1);
}
}
|
Output:
Four < Five < Six < Seven
class Gfg2 {
public static void main(String args[])
{
String gfg2 = String.join( " " , "My" , "name" , "is" , "Niraj" , "Pandey" );
System.out.println(gfg2);
}
}
|
Output:
My name is Niraj Pandey
class Gfg3 {
public static void main(String args[])
{
String gfg3 = String.join( "-> " , "Wake up" , "Eat" ,
"Play" , "Sleep" , "Wake up" );
System.out.println(gfg3);
}
}
|
Output:
Wake up-> Eat-> Play-> Sleep-> Wake up
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Dec, 2018
Like Article
Save Article