1. Introduction
StringJoiner is a new class added in Java 8 in java.util package. This class is very helpful to create string separated by delimiter and having a prefix and suffix. StringJoiner is used to create a sequence of characters separated by a delimiter. You can optionally provide a prefix and suffix to create the sequence of characters.
2. Constructor
StringJoiner(CharSequence delimiter) | This constructs a StringJoiner with a copy of delimiter and no characters in it. It will have no suffix and prefix. |
StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix) | This constructs a StringJoiner using copies of delimiter, prefix and suffix. It will have no characters in it. |
3. Example
import java.util.StringJoiner;
public class StrinhJoinerExample {
public static void main(String[] args) {
// StringJoiner with only delimiter
StringJoiner stringJoiner = new StringJoiner(",");
System.out.println("before adding any string: " + stringJoiner);
stringJoiner.add("James");
System.out.println("after adding one string: " + stringJoiner);
stringJoiner.add("carpenter");
System.out.println("after adding two strings: " + stringJoiner);
// StringJoiner with delimiter, suffix and prefix
StringJoiner stringJoiner2 = new StringJoiner(",", "[", "]");
stringJoiner2.add("Anna");
System.out.println("after adding one string: " + stringJoiner2);
stringJoiner2.add("Bird");
System.out.println("after adding two strings: " + stringJoiner2);
}
}
Output
before adding any string: after adding one string: James after adding two strings: James,carpenter after adding one string: [Anna] after adding two strings: [Anna,Bird]
4. Add a CharSequence
StringJoiner add(CharSequence value)
This method adds given CharSequence value to the StringJoiner value.
import java.util.StringJoiner;
public class StrinhJoinerExample {
public static void main(String[] args) {
// StringJoiner with delimiter, suffix and prefix
StringJoiner stringJoiner = new StringJoiner(",", "[", "]");
stringJoiner.add("One");
stringJoiner.add("Two");
stringJoiner.add("Three");
System.out.println("stringJoiner: " + stringJoiner);
}
}
Output
stringJoiner: [One,Two,Three]
5. Set empty value
StringJoiner setEmptyValue(CharSequence emptyValue)
This method is used to set the emptyValue of StringJoiner when it is empty, that is, no CharSequence has been added to it.
import java.util.StringJoiner;
public class StrinhJoinerExample {
public static void main(String[] args) {
// StringJoiner with delimiter, suffix and prefix
StringJoiner stringJoiner = new StringJoiner(",", "[", "]");
System.out.println("stringJoiner before adding anything: " + stringJoiner);
stringJoiner.setEmptyValue("Default value");
System.out.println("stringJoiner after setting empty value: " + stringJoiner);
}
}
Output
stringJoiner before adding anything: [] stringJoiner after setting empty value: Default value
6. length of StringJoiner
int length()
This method returns the length of the String representation of this StringJoiner.
import java.util.StringJoiner;
public class StrinhJoinerExample {
public static void main(String[] args) {
// StringJoiner with delimiter, suffix and prefix
StringJoiner stringJoiner = new StringJoiner(":", "[", "]");
stringJoiner.add("one");
stringJoiner.add("two");
stringJoiner.add("three");
System.out.println("length of stringJoiner" + stringJoiner + ": " + stringJoiner.length());
}
}
Output
length of stringJoiner[one:two:three]: 15
7. Merge StringJoiner
StringJoiner merge(StringJoiner value)
This method adds the content of the given StringJoiner without prefix and suffix if it is not empty.
import java.util.StringJoiner;
public class StrinhJoinerExample {
public static void main(String[] args) {
// StringJoiner with delimiter, suffix and prefix
StringJoiner stringJoiner = new StringJoiner(":", "[", "]");
stringJoiner.add("one");
stringJoiner.add("two");
stringJoiner.add("three");
System.out.println("stringJoiner: " + stringJoiner);
StringJoiner stringJoiner2 = new StringJoiner(",", "{", "}");
stringJoiner2.add("four");
stringJoiner2.add("five");
stringJoiner2.add("six");
System.out.println("stringJoiner2: " + stringJoiner2);
// merge
stringJoiner.merge(stringJoiner2);
System.out.println("stringJoiner after merge: " + stringJoiner);
}
}
Output
stringJoiner: [one:two:three]
stringJoiner2: {four,five,six}
stringJoiner after merge: [one:two:three:four,five,six]
8. toString()
- This method returns the current value of
StringJoinerwith prefix, delimiter and suffix. - If the
StringJoineris empty, the value set bysetEmptyValue()method will be returned. - If the
StringJoineris empty and no value is set bysetEmptyValue()method then suffix+prefix will be returned.
