Tuesday, August 9, 2022

StringJoiner Class in Java With Examples

In Java 8 StringJoiner class is added that can be used for joining the Strings separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.

Constructors in StringJoiner class

Java StringJoiner class has two 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.

Java StringJoiner class examples

Let us see few examples to understand how StringJoiner class is used to join Strings using a delimiter.

1. If you have three strings which you want to join and delimiter is ':' then it can be done using StringJoiner class as follows-

import java.util.StringJoiner;

public class StringJoinerDemo {
 public static void main(String[] args) {
  String str1 = "An";
  String str2 = "example";
  String str3 = "string";
  // providing delimiter
  StringJoiner sj = new StringJoiner(":");
  // adding strings that are to be joined
  sj.add(str1).add(str2).add(str3);
  System.out.println(sj.toString());
 }
}

Output

An:example:string

2. If you want to get the joined strings with suffix and prefix so that the end string looks like this - (An,example,string)

import java.util.StringJoiner;

public class StringJoinerDemo {
 public static void main(String[] args) {
  String str1 = "An";
  String str2 = "example";
  String str3 = "string";
  // providing delimiter and suffix, prefix
  StringJoiner sj = new StringJoiner(",", "(", ")");
  // adding strings that are to be joined
  sj.add(str1).add(str2).add(str3);
  System.out.println(sj.toString());
 }
}

Java StringJoiner class methods

  1. add(CharSequence newElement)- Adds a copy of the given CharSequence value as the next element of the StringJoiner value.
  2. length()- Returns the length of the String representation of this StringJoiner.
  3. merge(StringJoiner other)- Adds the contents of the passed StringJoiner object without prefix and suffix as the next element of the StringJoiner if it is non-empty.
  4. setEmptyValue(CharSequence emptyValue)- Sets the default sequence of characters to be used when StringJoiner is empty.
  5. toString()- Returns the current value as String, consisting of the prefix, the values added so far separated by the delimiter, and the suffix

Java StringJoiner setEmptyValue() method example

import java.util.StringJoiner;

public class StringJoinerDemo {
 public static void main(String[] args) {
  StringJoiner sj = new StringJoiner(":", "[", "]");
  System.out.println("StringJoiner- " + sj.toString());
  sj.setEmptyValue("No element is added yet");
  System.out.println("StringJoiner- " + sj.toString());
  sj.add("Adding").add("Values").add("Now");
  System.out.println("StringJoiner- " + sj.toString());
 }
}

Output

StringJoiner- []
StringJoiner- No element is added yet
StringJoiner- [Adding:Values:Now]

As you can see when default sequence is added using setEmptyValue() method that is displayed when no element is added. When elements are added then those elements are displayed as a joined string.

Java StringJoiner merge() method example

public class StringJoinerDemo {
 public static void main(String[] args) {
  StringJoiner sj1 = new StringJoiner(":", "[", "]");
  sj1.add("This").add("is").add("First").add("String");
  System.out.println("Length of sj1- " + sj1.length());
  StringJoiner sj2 = new StringJoiner("-", "{", "}");
  sj2.add("Second").add("String");
  System.out.println("Length of sj2- " + sj2.length());
  sj1 = sj1.merge(sj2);
  System.out.println("Merged String- " + sj1.toString());
  System.out.println("Length of Merged String- " + sj1.length());
 }
}

Output

Length of sj1- 22
Length of sj2- 15
Merged String- [This:is:First:String:Second-String]
Length of Merged String- 36

As you can see contents of the second StringJoiner is added without prefix and suffix. Delimiter of the second StringJoiner is retained.

That's all for this topic StringJoiner Class in Java With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related topics

  1. String in Java Tutorial
  2. Why Java String is Immutable
  3. String Comparison in Java equals(), compareTo(), startsWith() Methods
  4. Converting Char to String And String to Char in Java
  5. Java String Interview Questions And Answers

You may also like-

  1. Association, Aggregation And Composition in Java
  2. Private Methods in Java Interface
  3. LinkedHashSet in Java With Examples
  4. Phaser in Java Concurrency
  5. Why wait(), notify() And notifyAll() Must be Called Inside a Synchronized Method or Block
  6. Spring Expression Language (SpEL) With Examples
  7. Spring Integration With Quartz Scheduler
  8. Magic Methods in Python With Examples

No comments:

Post a Comment