StringJoiner toString() method in Java 8

Java 8Object Oriented ProgrammingProgramming

The toString() method of the StringJoiner class in Java8 is used to return the string representation of the StringJoiner.

The syntax of the toString() method is as follows:

String toString()

To work with the StringJoiner in Java 8, import the following package:

import java.util.StringJoiner;

The following is an example to implement StringJoiner toString() method in Java:

Example

 Live Demo

import java.util.StringJoiner;
public class Demo {
   public static void main(String[] args) {
      StringJoiner strJoin = new StringJoiner(" ");
      strJoin.add("One");
      strJoin.add("Two");
      strJoin.add("Three");
      strJoin.add("Four");
      strJoin.add("Five");
      System.out.println(strJoin.toString());
   }
}

output

One Two Three Four Five

Above, we have set the delimiter as a whitespace:

StringJoiner strJoin = new StringJoiner(" ");
raja
Updated on 30-Jul-2019 22:30:25

Advertisements