String join() In Java

String join() In Java | The join() method is in java.string class joins the string with the given delimiter. The join() method was introduced in JDK 1.8 version. There are two method variations of the Java string join method. Here, we will discuss how to join two strings in Java using the join() method.

The method details of the join() method are as follows:-

1) public static String join(CharSequence delimiter, CharSequence… substring)
2) public static String join(CharSequence delimiter, Iterable<? extends CharSequence> substring)

String.join() Java Method Implementation

The internal implementation of the java string join method is as follows:-

public static String join(CharSequence delimiter, 
                          CharSequence... elements) {
   Objects.requireNonNull(delimiter);
   Objects.requireNonNull(elements);
   // Number of elements not likely worth Arrays.stream overhead.
   StringJoiner joiner = new StringJoiner(delimiter);
   for (CharSequence cs: elements) {
      joiner.add(cs);
   }
   return joiner.toString();
}

In the above method,
Parameters:- delimiter: a sequence of characters that is used to separate each of the elements in the resulting string, elements: the elements to join together.
Returns:- Joined string
Throws:- NulPointerException if the substring or elements is null.

The internal implementation of the string join() method for the second variation is as follows:-

public static String join(CharSequence delimiter,
        Iterable<? extends CharSequence> elements) {
   Objects.requireNonNull(delimiter);
   Objects.requireNonNull(elements);
   StringJoiner joiner = new StringJoiner(delimiter);
   for (CharSequence cs: elements) {
      joiner.add(cs);
   }
   return joiner.toString();
}

In the above method,
Parameters:- delimiter: a sequence of characters that is used to separate each of the elements in the resulting string, elements: an Iterable that will have its elements joined together.
Returns:- Joined string
Throws:- NulPointerException if the substring or delimiter is null.

Java String.join() Example

Let us see an example of the String join() in Java. We took a delimiter as “.” and passed it to the Java string join() method.

public class Main {
   public static void main(String args[]) {
      String string = String.join(".", "www", "knowprogram", "com");
      System.out.println(string);
   }
}

Output:-

www.knowprogram.com

Example-2:- Let us another program of String.join() Java method to create a date & time string from the given string.

public class Main {
   public static void main(String[] args) {
      String time = String.join(":", "06", "05", "15");
      System.out.println("The time is = " + time);
      String date = String.join("-", "12", "10", "10");
      System.out.print("The date is = " + date);
   }
}

Output:-

The time is = 06:05:15
The date is = 12-10-10

Java String.join() Exception

This program is to show the exception thrown by the string.join() Java method.

public class Main {
   public static void main(String argvs[]) {
      String string = null;
      string = String.join(null, "know", "program", "website");
      System.out.println(string);
   }
}

Output:-

Exception in thread “main” java.lang.NullPointerException: Cannot invoke “java.lang.CharSequence.toString()” because “delimiter” is null
at java.base/java.lang.String.join(String.java:3225)
at Main.main(Main.java:5)

In the above program, we have passed null for the delimiter therefore the string.join() method throws NullPointerException. Similarly, if we pass null for the elements then also it will throw the same exception.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *