StringJoiner – Java 8 Feature

0
1209

Java 8 has introduced StringJoiner class which has helped a lot to all the developers dealing Strings and delimiters. It is used to construct a sequence of characters separated by a delimiter. Optionally a prefix can be added at start and suffix can be added at the end of String.

StringJoiner Constructors

StringJoiner(CharSequence delimiter)
Constructs a StringJoiner with no characters in it, with no prefix or suffix, and a copy of the supplied delimiter.

StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
Constructs a StringJoiner with no characters in it using copies of the supplied prefix, delimiter and suffix.

StringJoiner Methods

StringJoiner setEmptyValue(CharSequence emptyValue)
StringJoiner add(CharSequence newElement)
StringJoiner merge(StringJoiner other)
int length()
String toString()

Let’s look at the code sample for constructors and the methods of StringJoiner :

#1 . Join Strings with a delimiter

import java.util.StringJoiner;

public class HelloWorld {
  public static void main(String[] args) {
    
     StringJoiner sj = new StringJoiner(",");
     String joinedStrings = sj.add("hello").add("world").toString();
     System.out.println("Delimiter joined String: " + joinedStrings);
  }
}

Output : Delimiter joined String: hello,world

#2. Join Strings with delimiter, prefix and suffix

import java.util.StringJoiner;

public class HelloWorld {
  public static void main(String[] args) {
    
    StringJoiner sj = new StringJoiner(",", "[", "]");
    String joinedStrings = sj.add("Shalini").add("Goyal").toString();
    System.out.println("With delimiter, Prefix and Suffix: " + joinedStrings);
  }
}

Output: With delimiter, Prefix and Suffix: [Shalini,Goyal]

#3. merge() and length() methods

import java.util.StringJoiner;

public class HelloWorld {
  public static void main(String[] args) {
    
    StringJoiner sj1 = new StringJoiner(",");
    String joinedStrings1 = sj1.add("Apple").add("Oranges").toString();
   
    StringJoiner sj2 = new StringJoiner(":");
    String joinedStrings2 = sj2.add("10").add("20").toString();
   
    String mergedString = sj1.merge(sj2).toString();
    System.out.println("Merged String : " + mergedString);
    
    System.out.println("Length of Merged String : " + mergedString.length());
    
  }
}

Hope you found the explanation and examples for StringJoiner useful. This feature of Java8 is really easy to implement.

Previous articleData Mining and Big Data
Next articleHow to Split String in Java – with Code Sample
I have spent almost 10 years playing around Java and related technologies. I love to write on different topics and would be very much willing to hear back your feedback/suggestions on them. This site is a medium to share my knowledge with the Java folks and grow further. My other interests include traveling, driving, swimming and dance. But yes, my web site has become my passion over the time :) I live in Scotland and travel to India often, my roots being there.

NO COMMENTS

LEAVE A REPLY