Java - String join() method



The Java String join() method concatenates the current string with the delimiter and returns the resultant string value. A string delimiter is a sequence of one or more characters used in plain text to define the boundary between separate, independent regions.

Note − Let's assume that we have a string "Hello, World" separated by a comma. The comma is the delimiter here.

The join() method accepts two different parameters that hold the value of the delimiter and string values. It throws an exception if the given delimiter is null.

This method have two polymorphic variants with different parameters. The syntaxes of both the polymorphic variants are given below.

Syntax

Following is the syntax of the Java String join() method −

Public static String join(CharSequence delimiter, CharSequence… elements)// first syntax
Public static String join(CharSequence delimiter, Iterable<? Extends CharSequence> elements)// second parameter

Parameters

  • delimiter − the delimiter that separates each element.

  • elements − the elements to join together.

Return Value

This method returns a new String which is a combined value of the of copies of the current string and the given objects separated by the specified delimiter.

Example

If the given delimiter value is not null, the join() method concatenates the string with the delimiter.

In the following program, we are creating the char sequence with the values of “Hello” and “World”. Then, using the join() method, we are trying to concatenate the current string with the delimiter "-" value.

import java.lang.*;
public class Join {
   public static void main(String[] args) {
      
      //instantiate the string class
      CharSequence str1 = "Hello";
      CharSequence str2 = "World";
      System.out.println("The given char sequence values are: " + str1 + " and " + str2);
      
      //initialize the delimiter
      CharSequence delimiter = "-";
      System.out.println("The given delimiter is '" + delimiter + "'");
      
      //using the join() method
      System.out.println("The new concatenated string is: " + String.join(delimiter, str1, str2));
   }
}

Output

On executing the above program, it will produce the following result −

The given char sequence values are: Hello and World
The given delimiter is '-'
The new concatenated string is: Hello-World

Example

If the given delimiter is null, this method throws a NullPointerException.

In the following program, we create the char sequence with the value “Tutorials” and “Point”. Using the join() method, we are trying to concatenate the char sequence with the delimiter “&”.

public class Join {
   public static void main(String[] args) {
      try {
         
         //instantiate the string class
         CharSequence str1 = "Tutorials";
         CharSequence str2 = "Point";
         System.out.println("The given char sequence values are: " + str1 + " and " + str2);
         
         //initialize the delimiter with null value
         CharSequence delimiter = null;
         System.out.println("The given delimiter is '" + delimiter + "'");
         
         //using the join() method
         System.out.println("The new concatenated string is: " + String.join(delimiter, str1, str2));
      } catch(NullPointerException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

Following is the output of the above program −

The given char sequence values are: Tutorials and Point
The given delimiter is 'null'
java.lang.NullPointerException: Cannot invoke "java.lang.CharSequence.toString()" because "delimiter" is null
	at java.base/java.lang.String.join(String.java:3228)
	at com.tutorialspoint.String.Join.main(Join.java:13)
Exception: java.lang.NullPointerException: Cannot invoke "java.lang.CharSequence.toString()" because "delimiter" is null

Example

Using the join() method, we can format the current time in the proper format.

In the following example, we are creating an object of the Date class. Then, we get the current hours, minutes, and seconds and convert them into the char sequence using the String.valueOf() method. Using the join() method, we are trying to concatenate the hours, minutes, and seconds with the delimiter “:“ value.

import java.util.Date;
import java.lang.*;
public class Join {
   public static void main(String[] args) {
      
      //create an object of date class
      Date date = new Date();
      int hour = date.getHours();
      int minute = date.getMinutes();
      int second = date.getSeconds();
      System.out.println("Hours: " + hour);
      System.out.println("Minutes: " + minute);
      System.out.println("Seconds: " + second);
      
      //convert int to char sequence
      CharSequence hh = String.valueOf(hour);
      CharSequence mm = String.valueOf(minute);
      CharSequence ss = String.valueOf(second);
      
      //initialize the delimiter
      CharSequence delimiter = " : ";
      
      //using join() method
      String current_time = String.join(delimiter, hh, mm, ss);
      System.out.println("The current time is: " + current_time);
   }
}

Output

The above program, produces the following output −

The given string is: Hello
Length of the string is: 5
The string is not an empty string

Example

In this program, we are creating an ArrayList as an iterable CharSequence, and adding values “Java” and “Programming” to it. Using the join() method, we are trying to concatenate the ArrayList with the given delimiter “ # ” value.

import java.util.ArrayList;
import java.util.List;
public class Join {
   public static void main(String[] args) {
      
      //create an arrayList
      List list = new ArrayList<>();
      
      //adding element to it using add() method
      list.add("Java");
      list.add("Programming");
      System.out.println("The given arrayList is: " + list);
      
      //initialize the delimiter value
      CharSequence dm = " # ";
      System.out.println("The given delimiter is = " + dm);
      String new_str = String.join(dm, list);
      System.out.println("The result is: " + new_str);
   }
}

Output

After executing the above program, it will produce the following output −

The given arrayList is: [Java, Programming]
The given delimiter is =  # 
The result is: Java # Programming
java_lang_string.htm
Advertisements