Java

Java 8 – StringJoiner With Example

A new class called StringJoiner is added to the java.util package in version 8. By using this class, we may combine several strings using the supplied delimiter. We can also add a prefix and a suffix to the combined texts. We’ll examine a number of StringJoiner class examples in this lesson, and we’ll also see the class’s methods at the conclusion.

Java StringJoiner Example 1: Joining strings by specifying delimiter

In this example, we are concatenating multiple strings using StringJoiner. While creating the instance of StringJoiner, we have specified the delimiter as hyphen(-).

import java.util.StringJoiner;  
public class Example {  
    public static void main(String[] args) {  
    	// Passing Hyphen(-) as delimiter
        StringJoiner mystring = new StringJoiner("-");    
          
        // Joining multiple strings by using add() method  
        mystring.add("Logan");  
        mystring.add("Magneto");  
        mystring.add("Rogue");  
        mystring.add("Storm");  
                  
        // Displaying the output String
        System.out.println(mystring);  
    }  
}

Output:

Logan-Magneto-Rogue-Storm

Java StringJoiner Example 2: Adding prefix and suffix to the output String

import java.util.StringJoiner;  
public class Example {  
    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("Negan");  
        mystring.add("Rick");  
        mystring.add("Maggie");  
        mystring.add("Daryl");  
                  
        // Displaying the output String
        System.out.println(mystring);  
    }  
}

Output:

(Negan,Rick,Maggie,Daryl)

StringJoiner Example 3: Merging two StringJoiner objects

import java.util.StringJoiner;  
public class Example {  
   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("Negan");  
	mystring.add("Rick");  
	mystring.add("Maggie");  
	mystring.add("Daryl");  

	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("Sansa");  
	myanotherstring.add("Imp");  
	myanotherstring.add("Jon");  
	myanotherstring.add("Ned"); 

	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: (Negan,Rick,Maggie,Daryl)
Second String: preSansa-Imp-Jon-Nedsuff
(Negan,Rick,Maggie,Daryl,Sansa-Imp-Jon-Ned)

We have seen the add() and merge() methods of the StringJoiner class in the examples above. Let’s examine the other methods in this class.

StringJoiner Example: setEmptyValue(), length() and toString() methods

import java.util.StringJoiner;  
public class Example {  
    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("This is a default String");  
        
        /* 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("Apple");  
        mystring.add("Banana"); 
        mystring.add("Orange");
        mystring.add("Kiwi");
        mystring.add("Grapes");
        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: This is a default String
Apple,Banana,Orange,Kiwi,Grapes
Length of the StringJoiner: 31
Apple,Banana,Orange,Kiwi,Grapes

 

 

Related Articles

Leave a Reply

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

Back to top button