Important Points
- Any class that implements CharSequece can be passed to join().
- Any iterable that implements CharSequence, if passed, its elements will be joined.
- String, StringBuffer, CharBuffer, etc., are CharSequence as these classes implement it.
Java String Join() method Signature
This method is used to return a new string which is composed of copies of the CharSequence elements joined together with a copy of the specified delimiter. The delimiter is passed as the first argument to this method and is used to join multiple strings. In case an element is null, then “null” is added.
Syntax
public static String join(CharSequence delimiter, CharSequence element-1,...,CharSequence element-2)
OR
public static String join
(CharSequence delimiter, Iterable<? extends CharSequence> element)
Parameters:
delimiter- delimiter which is to attached with each element
element- string/char that is to be attached
Examples
Code
public class StringJoinExample{
public static void main(String args[]){
//delimiter is passed as the first argument
String string=String.join("-","Hello","how","are","you?");
System.out.println(string);
}
}
Output
Hello-how-are-you?
Code
import java.util.List;
import java.util.Arrays;
public class StringJoinExample{
public static void main(String args[]){
//Converting an array of String to the list
List list<String> = Arrays.asList("Mango", "Apple", "Apricot");
String string = String.join(" : ", list);
System.out.println(string);
}
}
Output
Mango : Apple : Apricot
Code
public class StringJoinExample
{
public static void main(String argvs[])
{
String string = null;
// passing null as the element
string = String.join("-", null, "Eat", "your", "veggies");
System.out.println(string);
}
}
Output
null-Eat-your-veggies
Frequently Asked Questions
How do we specify the delimiter in the string.join() method?
The delimiter is passed as the first argument in the string.join() method.
Name some predefined methods of the Java String class?
Some of the predefined methods of the Java String class are charAt(), compareTo(), equals(), etc.
Conclusion
In this article, we have extensively discussed the Join() method of the String class in the Java programming language.
After reading about Join() method, are you not feeling excited to read/explore more articles on Strings? Don't worry; Coding Ninjas has you covered. To learn about special string functions in Java, strings in Java, and how to print all the permutations of a string.
Recommended problems -
If you wish to enhance your skills in Data Structures and Algorithms, Competitive Programming, JavaScript, and many more, then you should check out our Guided path column at Coding Ninjas Studio. We at Coding Ninjas Studio organize many contests in which you can participate. You can also prepare for the contests and test your coding skills by giving the mock test series available. In case you have just started the learning process, and your dream is to crack major tech giants like Amazon, Microsoft, etc., then you should check out the most frequently asked problems and the interview experiences of your seniors that will surely help you in landing a job in your dream company.
Do upvote if you find the blogs helpful.
Happy Learning!