Java StringJoiner Example

StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.
Learn and master in Java 8 features at Java 8 Tutorial with Examples.

Java StringJoiner Example

The following example shows:
  • Simple Delimiters Example
  • adding prefix and suffix
  • Merge Two StringJoiner
import java.util.StringJoiner;

/**
 * Java added a new final class StringJoiner in java.util package. It is used to construct
 * a sequence of characters separated by a delimiter
 * @author RAMESH
 *
 */
public class Main {

    public static void main(String[] args) {
        delimiterDemonstration();
        addingPrefixAndSuffix();
        mergeTwoStringJoiner();
    }

    private static void delimiterDemonstration() {
        StringJoiner joinNames = new StringJoiner(","); // passing comma(,) as delimiter
        // Adding values to StringJoiner
        joinNames.add("John");
        joinNames.add("Tom");
        joinNames.add("Tony");
        joinNames.add("Cap");
        System.out.println(joinNames);

        joinNames = new StringJoiner("|"); // passing pipe(|) as delimiter

        // Adding values to StringJoiner
        joinNames.add("John");
        joinNames.add("Tom");
        joinNames.add("Tony");
        joinNames.add("Cap");
        System.out.println(joinNames);
    }

    private static void addingPrefixAndSuffix() {
        // passing comma(,) and
        // square-brackets as
        // delimiter
        StringJoiner joinNames = new StringJoiner(",", "[", "]");
        // Adding values to StringJoiner
        joinNames.add("John");
        joinNames.add("Tom");
        joinNames.add("Tony");
        joinNames.add("Cap");

        System.out.println(joinNames);
    }

    private static void mergeTwoStringJoiner() {
        // passing comma(,) and square-brackets as delimiter
        StringJoiner joinNames = new StringJoiner(",", "[", "]");

        // Adding values to StringJoiner  
        joinNames.add("John");
        joinNames.add("Tom");

        // Creating StringJoiner with :(colon) delimiter  
        StringJoiner joinNames2 = new StringJoiner(":", "[", "]"); // passing colon(:) and square-brackets as delimiter   

        // Adding values to StringJoiner  
        joinNames2.add("Tony");
        joinNames2.add("Cap");

        // Merging two StringJoiner  
        StringJoiner merge = joinNames.merge(joinNames2);
        System.out.println(merge);
    }
}
Output:
John,Tom,Tony,Cap                                                                                                 
John|Tom|Tony|Cap                                                                                                 
[John,Tom,Tony,Cap]                                                                                               
[John,Tom,Tony:Cap] 

Java StringJoiner Class Methods Example

The following example demonstrates the usage of StringJoiner class APIs with examples:
import java.util.StringJoiner;

/**
 * Java added a new final class StringJoiner in java.util package. It is used to construct
 * a sequence of characters separated by a delimiter
 * @author RAMESH
 *
 */
public class Main {

    public static void main(String[] args) {
        stringJoinerMethods();
    }


    private static void stringJoinerMethods() {
        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("Rahul");
        joinNames.add("Raju");
        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("Sorabh");
        System.out.println(joinNames);

        // Returns length  
        int newLength = joinNames.length();
        System.out.println("New Length: " + newLength);
    }
}
Output:
It is empty                                                                                                             
Rahul,Raju                                                                                                              
Length: 10                                                                                                              
Rahul,Raju                                                                                                              
Character at index 3: u                                                                                                 
Rahul,Raju,Sorabh                                                                                                       
New Length: 17

Reference

Comments