Java StringJoiner - toString() Method
The java.util.StringJoiner.toString() method returns the current value, consisting of the prefix, the values added so far separated by the delimiter, and the suffix, unless no elements have been added in which case, the prefix + suffix or the emptyValue characters are returned.
Syntax
public String toString()
Parameters
No parameter is required.
Return Value
Returns the string representation of this StringJoiner.
Exception
NA
Example:
In the example below, the java.util.StringJoiner.toString() method returns the content of the given StringJoiner.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a StringJoiner object StringJoiner joinNames = new StringJoiner(", ", "[", "]"); //Adding values to joinNames joinNames.add("John"); joinNames.add("Marry"); joinNames.add("Kim"); joinNames.add("Jo"); joinNames.add("Ramesh"); //printing joinNames using toString() method System.out.print("joinNames contains: "); System.out.println(joinNames.toString()); } }
The output of the above code will be:
joinNames contains: [John, Marry, Kim, Jo, Ramesh]
❮ Java.util - StringJoiner