Table of contents
1.
Introduction
2.
Java String join()
3.
Important Points
4.
Java String Join() method Signature
5.
Examples
6.
Frequently Asked Questions
6.1.
How do we specify the delimiter in the string.join() method?
6.2.
Name some predefined methods of the Java String class?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

String Join()

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java is a very powerful programming language. It contains many predefined data structures used to store data in different forms, such as arrays, strings, etc. A string in Java is defined as an object that represents a sequence of char values. They are represented using double-quotes. The Java string class provides a large number of predefined methods which allow us to perform operations on strings such as concat(), equals(), etc. One such method is join(), in this blog, we will discuss it in detail.

Java String join()

Since JDK 1.8, join() method has been included in the Java string class. The java.lang.string.join() method is used to concatenate a given element with the delimiter. The method returns a concatenated string. It accepts two parameters which are as follows:

  • Delimiter: The delimiter to be joined with the elements
  • Elements: Elements to be joined

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 AlgorithmsCompetitive ProgrammingJavaScript, 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!

Live masterclass