Java 8 StringJoiner class

Java 8 introduced a new class StringJoiner in the java.util package which provides the facility to join more than one strings with the specified delimiter. We can also use prefix and suffix to the final string while joining multiple strings.

Joining strings by specifying delimiter

package com.w3schools;

import java.util.StringJoiner;


public class TestExample {
      public static void main(String args[]){
	//Passing comma as delimiter
        StringJoiner mystring = new StringJoiner(",");    
          
        //Joining multiple strings by using add() method  
        mystring.add("Jai");  
        mystring.add("Vikas");  
        mystring.add("Ajay");  
        mystring.add("Vivek");  
                  
        //Displaying the output String
        System.out.println(mystring);  
     }
}

Output

Jai,Vikas,Ajay,Vivek

Adding prefix and suffix to the output String

package com.w3schools;

import java.util.StringJoiner;


public class TestExample {
  public static void main(String args[]){
	/* Passing comma(,) as delimiter and opening bracket
    	 * "(" as prefix and closing bracket ")" as suffix
    	 */
        StringJoiner mystring = new StringJoiner(",", "(", ")");     
          
        //Joining multiple strings by using add() method  
        mystring.add("Jai");  
        mystring.add("Vikas");  
        mystring.add("Ajay");  
        mystring.add("Vivek");  
                  
        //Displaying the output String
        System.out.println(mystring);  
   }
}

Output

(Jai,Vikas,Ajay,Vivek)

Merging two StringJoiner objects

package com.w3schools;

import java.util.StringJoiner;


public class TestExample {
	public static void main(String args[]){
		/* Passing comma(,) as delimiter and opening bracket
		 * "(" as prefix and closing bracket ")" as suffix
		 */
		StringJoiner mystring = new StringJoiner(",", "(", ")");    

		mystring.add("Jai");  
		mystring.add("Vivek");  
		mystring.add("Mahesh");  
		mystring.add("Vishal");  

		System.out.println("First String: "+mystring);

		/* Passing hyphen(-) as delimiter and string "pre"
		 * as prefix and string "suff" as suffix
		 */
		StringJoiner myanotherstring = new StringJoiner("-", "pre", "suff");    

		myanotherstring.add("Naren");  
		myanotherstring.add("Vikas");  
		myanotherstring.add("Sahil");  
		myanotherstring.add("Hemant"); 

		System.out.println("Second String: "+myanotherstring);

		/* Merging both the strings  
		 * The important point to note here is that the output string will be 
		 * having the delimiter prefix and suffix of the first string (the string
		 * which is calling the merge method of StringJoiner)
		 */
		StringJoiner mergedString = mystring.merge(myanotherstring);   
		System.out.println(mergedString);  
	   }  
}

Output

First String: (Jai,Vivek,Mahesh,Vishal)
Second String: preNaren-Vikas-Sahil-Hemantsuff
(Jai,Vivek,Mahesh,Vishal,Naren-Vikas-Sahil-Hemant)

StringJoiner methods example

package com.w3schools;

import java.util.StringJoiner;


public class TestExample {
  public static void main(String args[]){
	//Comma(,) as delimiter
        StringJoiner mystring = new StringJoiner(",");   
          
        /* Using setEmptyValue() method, we can set the default value
         * of a StringJoiner instance, so if the StringJoiner is empty
         * and we print the value of it, this default value will be
         * displayed
         */
        mystring.setEmptyValue("Hello w3schools");  
        
        /* We have not added any string to StringJoiner yet so
         * this should display the default value of StringJoiner
         */
        System.out.println("Default String: "+mystring);  
          
          
        // Adding strings to StringJoiner  
        mystring.add("Jai");  
        mystring.add("Mahesh"); 
        mystring.add("Vivek");
        mystring.add("Vikas");
        mystring.add("Hemant");
        System.out.println(mystring);  
          
        /* The length() method of StringJoiner class returns the 
         * length of the string (the number of characters in the 
         * StringJoiner instance)
         */
        int length = mystring.length();  
        System.out.println("Length of the StringJoiner: "+length);  
          
        /* The toString() method is used for converting a StringJoiner
         *  instance to a String. 
         */
        String s = mystring.toString();  
        System.out.println(s);     
  }  
}

Output

Default String: Hello w3schools
Jai,Mahesh,Vivek,Vikas,Hemant
Length of the StringJoiner: 29
Jai,Mahesh,Vivek,Vikas,Hemant