Pages

Java String Joiner


StringJoiner Class

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, you can create string by passing delimiters like comma(,), hyphen(-) etc. You can also pass prefix and suffix to the char sequence.



Java StringJoiner Example

import java.util.StringJoiner;
public class StringJoinerExample {
      public static void main(String[] args) {
            StringJoiner joinNames = new StringJoiner(",");
            joinNames.add("Rahul");
            joinNames.add("Raju");
            joinNames.add("Peter");
            joinNames.add("Raheem");
            System.out.println(joinNames);
      }
}

Output :

Rahul, Raju, Peter, Raheem

Java StringJoiner Example: adding prefix and suffix

import java.util.StringJoiner;
public class StringJoinerExample {
      public static void main(String[] args) {
            StringJoiner joinNames = new StringJoiner(",", "[", "]");
            joinNames.add("Rahul");
            joinNames.add("Raju");
            joinNames.add("Peter");
            joinNames.add("Raheem");
            System.out.println(joinNames);
      }
}

Output :

[Rahul, Raju, Peter, Raheem]

StringJoiner Example: Merge Two StringJoiner


The merge() method merges two StringJoiner objects excluding of prefix and suffix of second StringJoiner object.
import java.util.StringJoiner;
public class StringJoinerExample {
      public static void main(String[] args) {
            StringJoiner joinNames = new StringJoiner(",", "[", "]");
            joinNames.add("Rahul");
            joinNames.add("Raju");
            StringJoiner joinNames2 = new StringJoiner(":", "[", "]");
            joinNames2.add("Peter");
            joinNames2.add("Raheem");
            StringJoiner merge = joinNames.merge(joinNames2);
            System.out.println(merge);
      }
}

Output :

[Rahul, Raju, Peter : Raheem]

StringJoiner Example: StringJoiner Methods

import java.util.StringJoiner;
public class StringJoinerExample {
      public static void main(String[] args) {
            StringJoiner joinNames = new StringJoiner(",");
            System.out.println(joinNames);
            joinNames.setEmptyValue("It is empty");
            System.out.println(joinNames);
            joinNames.add("Rahul");
            joinNames.add("Raju");
            System.out.println(joinNames);
            int length = joinNames.length();
            System.out.println("Length: "+length);
            String str = joinNames.toString();
            System.out.println(str);
            char ch = str.charAt(3);
            System.out.println("Character at index 3: "+ch);
            joinNames.add("Sorabh");
            System.out.println(joinNames);
            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