Signup/Sign In
LAST UPDATED: JUNE 26, 2020

Java 8 StringJoiner Class

Java StringJoiner class is included in the Java 8 version that allows us to construct a sequence of characters separated by a delimiter.

It is located in java.util package and used to provide utility to create a string by a user-defined delimiter. the delimiter can be anything like comma (,), colon(:) etc. We can also pass suffix and prefix to the string joiner object.

Suppose, we have a multiword string and want to separate each word by a delimiter(comma, semicolon) then StringJoiner is suitable for this purpose. The Declaration of the class is given below.

Declaration

public final class StringJoiner extends Object

StringJoiner class Constructors

The StringJoiner class contains two constructors that are the following:

Constructor

Description

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 the 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

The following table contains methods of StringJoiner class.

Method

Description

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.

public String toString()

It returns the current value, consisting of the prefix, and the suffix or the empty value characters are returned.

Time for an Example:

Let's take an example to create a string using the StringJoiner class. Here we used comma delimiter to separate string characters. See the below example.

import java.util.StringJoiner;
public class STDemo {
	public static void main(String[] args) {
		StringJoiner sj = new StringJoiner(",");
		sj.add("India");
		sj.add("China");
		sj.add("US");
		sj.add("UK");
		System.out.println(sj);

	}

}


India,China,US,UK

Time for another Example:

StringJoiner allows suffix and prefix optionally to enclose the string object. In this example, we are creating String by setting parenthesis as suffix and prefix and colon as a delimiter.

import java.util.StringJoiner;
public class STDemo {
	public static void main(String[] args) {
		StringJoiner sj = new StringJoiner(":","(",")");
		sj.add("India");
		sj.add("China");
		sj.add("US");
		sj.add("UK");
		System.out.println(sj);
	}

}


(India:China:US:UK)

Example: Get length of String

The StringJoiner class provides a length() method to get the length of the string. In this example, we are getting length of the string.

import java.util.StringJoiner;
public class STDemo {
	public static void main(String[] args) {
		StringJoiner sj = new StringJoiner(":","(",")");
		sj.add("India");
		sj.add("China");
		sj.add("US");
		sj.add("UK");
		System.out.println(sj);
		System.out.println("String length : "+sj.length());

	}

}


(India:China:US:UK)
String length : 19

Example: Merge Two Strings

StringJoiner merge() method is used to merge two stringjoiner objects into one. In this example, we are merging two strings and getting a single string object.

import java.util.StringJoiner;
public class STDemo {
	public static void main(String[] args) {
		StringJoiner sj = new StringJoiner(":","(",")");
		sj.add("India");
		sj.add("China");
		sj.add("US");
		sj.add("UK");
		System.out.println(sj);
		StringJoiner sj2 = new StringJoiner(":","(",")");
		sj.add("Germany");
		sj.add("Russia");
		sj = sj.merge(sj2);
		System.out.println(sj);

	}

}


(India:China:US:UK)
(India:China:US:UK:Germany:Russia)



About the author:
I am a Java developer by profession and Java content creator by passion. I have over 5 years of experience in Java development and content writing. I like writing about Java, related frameworks, Spring, Springboot, etc.