Java StringJoiner Class

Last Updated : 7 Aug 2026

Java added a new final class StringJoiner in java.util package. It is used to construct a sequence of characters separated by a delimiter. Now, we can create string by passing delimiters like comma(,), hyphen(-) etc. We can also pass prefix and suffix to the char sequence. It is especially useful when constructing formatted strings dynamically.

Key Features

  • Delimiters: It defines a separator between two strings.
  • Prefix and Suffix: It allows adding optional characters before and after the final joined string.
  • Chaining: Chaining calls to add elements for a clean and concise implementation.

StringJoiner Class Constructors

ConstructorDescription
public StringJoiner(CharSequence delimiter)It constructs a StringJoiner with no characters in it, with no prefix or suffix, and a copy of the supplied delimiter. It throws NullPointerException if delimiter is null.
public StringJoiner(CharSequence delimiter,CharSequence prefix,CharSequence suffix)It constructs a StringJoiner with no characters in it using copies of the supplied prefix, delimiter and suffix. It throws NullPointerException if prefix, delimiter, or suffix is null.

StringJoiner Class Methods

MethodDescription
public StringJoiner add(CharSequence newElement)It adds a copy of the given CharSequence value as the next element of the StringJoiner value. If newElement is null,"null" is added.
public StringJoiner merge(StringJoiner other)It adds the contents of the given StringJoiner without prefix and suffix as the next element if it is non-empty. If the given StringJoiner is empty, the call has no effect.
public int length()It returns the length of the String representation of this StringJoiner.
public StringJoiner setEmptyValue(CharSequence emptyValue)It sets the sequence of characters to be used when determining the string representation of this StringJoiner and no elements have been added yet, that is, when it is empty.

Example: Printing Values Separated by Delimiter

Java

import java.util.StringJoiner;  
public class Main {  
    public static void main(String[] args) {  
        StringJoiner joinNames = new StringJoiner(", ");// passing comma(,) as delimiter  
        // Adding values to StringJoiner  
        joinNames.add("James");  
        joinNames.add("Oliver");  
        joinNames.add("Peter");  
        joinNames.add("Robert");  
        System.out.println(joinNames);  
    }  
}  
Compile and Run

Output:

James, Oliver, Peter, Robert

Example: Adding Prefix and Suffix

Java

import java.util.StringJoiner;  
public class Main {  
    public static void main(String[] args) {  
        StringJoiner joinNames = new StringJoiner(", ", "[", "]");// passing comma(,) and square-brackets as delimiter 
        // Adding values to StringJoiner  
        joinNames.add("James");  
        joinNames.add("Oliver");  
        joinNames.add("Peter");  
        joinNames.add("Robert");  
        System.out.println(joinNames);  
    }  
}
Compile and Run

Output:

[James, Oliver, Peter, Robert]

Example: Merge Two StringJoiner

The merge() method merges two StringJoiner objects excluding of prefix and suffix of second StringJoiner object.

Java

import java.util.StringJoiner;  
public class Main {  
    public static void main(String[] args) {  
        StringJoiner joinNames = new StringJoiner(", ", "[", "]");   // passing comma(,) and square-brackets as delimiter           
        // Adding values to StringJoiner  
        joinNames.add("Robert");  
        joinNames.add("Jack"); 
        // Creating StringJoiner with :(colon) delimiter  
        StringJoiner joinNames2 = new StringJoiner(": ", "[", "]");  // passing colon(:) and square-brackets as delimiter   
        // Adding values to StringJoiner  
        joinNames2.add("Peter");  
        joinNames2.add("Olive");  
        // Merging two StringJoiner  
        StringJoiner merge = joinNames.merge(joinNames2);   
        System.out.println(merge);  
    }  
}  
Compile and Run

Output:

[Robert, Jack, Peter: Olive]

Example: Using Different StringJoiner Methods

Java

import java.util.StringJoiner;  
public class Main {  
    public static void main(String[] args) {  
  StringJoiner joinNames = new StringJoiner(", "); // passing comma(,) as delimiter  
        // Prints nothing because it is empty  
        System.out.println(joinNames);  
        // We can set default empty value.  
        joinNames.setEmptyValue("It is empty");  
        System.out.println(joinNames);            
        // Adding values to StringJoiner  
        joinNames.add("Jack");  
        joinNames.add("Robert");  
        System.out.println(joinNames);  
        // Returns length of StringJoiner  
        int length = joinNames.length();  
        System.out.println("Length: "+length);  
        // Returns StringJoiner as String type   
        String str = joinNames.toString();  
        System.out.println(str);  
        // Now, we can apply String methods on it  
        char ch = str.charAt(3);  
        System.out.println("Character at index 3: "+ch);  
        // Adding one more element   
        joinNames.add("Tom");  
        System.out.println(joinNames);  
        // Returns length  
        int newLength = joinNames.length();  
        System.out.println("New Length: "+newLength);  
    }  
}  
Compile and Run

Output:

It is empty
Jack, Robert
Length: 12
Jack, Robert
Character at index 3: k
Jack, Robert, Tom
New Length: 17