April 24, 2022

Java StringJoiner Class With Method Examples

In Java 8 a new class StringJoiner has been added that can be used for joining the Strings using the specified delimiter. There is also an option to pass prefix and suffix to be added to the final joined String.

Read Also: Java String join() Method With Examples

Java StringJoiner class 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 examples

1. Constructing a String with colon ":" as delimiter.

public class JoinStrings {
  public static void main(String[] args) {
    // Specifying delimiter 
    StringJoiner sj = new StringJoiner(":");
    // adding values
    sj.add("Java");
    sj.add("Python");
    sj.add("Scala");
    System.out.println("Final String- " + sj.toString());
  }
}
Output
Final String- Java:Python:Scala

2. Constructing a String with colon "|" as delimiter, “[“ and “]” as prefix and suffix.

public class JoinStrings {
  public static void main(String[] args) {
    // Specifying delimiter and prefix, suffix 
    StringJoiner sj = new StringJoiner("|", "[", "]");
    // adding values
    sj.add("Java");
    sj.add("Python");
    sj.add("Scala");
    System.out.println("Final String- " + sj.toString());
  }
}
Output
Final String- [Java|Python|Scala]

Java StringJoiner methods

  • add(CharSequence newElement)- Adds a copy of the given CharSequence value as the next element of the StringJoiner value.
  • length()- Returns the length of the String representation of this StringJoiner.
  • merge(StringJoiner other)- Adds the contents of the given StringJoiner without prefix and suffix as the next element if it is non-empty.
  • setEmptyValue(CharSequence emptyValue)- Sets the sequence of characters to be used as default when it is empty.
  • toString()- Returns the current value as String

StringJoiner methods – setEmptyValue example

public class JoinStrings {
  public static void main(String[] args) {
    // Specifying delimiter and prefix, suffix 
    StringJoiner sj = new StringJoiner("|", "[", "]");
    System.out.println("Final String- " + sj.toString());
    //Default String
    sj.setEmptyValue("Default String");
    System.out.println("Final String- " + sj.toString());
    // adding values
    sj.add("Java");
    sj.add("Python");
    sj.add("Scala");
    System.out.println("Final String- " + sj.toString());
  }
}
Output
Final String- []
Final String- Default String
Final String- [Java|Python|Scala]

As you can see when a default String is added using setEmptyValue() method that is displayed. When elements are added using add() method then those values are displayed.

StringJoiner methods – merge example

public class JoinStrings {
  public static void main(String[] args) {
    // Specifying delimiter and prefix, suffix 
    StringJoiner sj1 = new StringJoiner("|", "[", "]");
    StringJoiner sj2 = new StringJoiner("-", "(", ")");
    // adding values
    sj1.add("Java");
    sj1.add("Python");
    sj1.add("Scala");
    System.out.println("StringJoiner 1- " + sj1.toString());
    sj2.add("Golang");
    sj2.add("Kotlin");
    sj2.add("Clojure");
    System.out.println("StringJoiner 2- " + sj2.toString());
    // merging sj2 to sj1
    sj1.merge(sj2);
    System.out.println("Merged String- " + sj1.toString());
    System.out.println("Merged String length- " + sj1.length());
  }
}
Output
StringJoiner 1- [Java|Python|Scala]
StringJoiner 2- (Golang-Kotlin-Clojure)
Merged String- [Java|Python|Scala|Golang-Kotlin-Clojure]
Merged String length- 41

When the StringJoiner objects are merged, if the other StringJoiner is using a different delimiter, then elements from the other StringJoiner are concatenated with that delimiter. Prefix and suffix of the StringJoiner object that is passed as parameter are discarded, delimiter of the calling StringJoiner are used.

That's all for the topic Java StringJoiner Class With Method Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment