StringJoiner toString() method in Java
The toString() of StringJoiner is used to convert StringJoiner to String. It 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()
Returns: This method returns the string representation of this StringJoiner
Below programs illustrate the toString() method:
Example 1: To demonstrate toString() with delimeter ” ”
// Java program to demonstrate // toString() method of StringJoiner import java.util.StringJoiner; public class GFG { public static void main(String[] args) { // Creating StringJoiner with delimeter " " StringJoiner str = new StringJoiner(" "); // Adding elements in the StringJoiner str.add("Geeks"); str.add("for"); str.add("Geeks"); // Print the StringJoiner // using toString() method System.out.println("StringJoiner: " + str.toString()); } } |
StringJoiner: Geeks for Geeks
Example 2: To demonstrate toString() with delimeter “, ”
// Java program to demonstrate // toString() method of StringJoiner import java.util.StringJoiner; public class GFG { public static void main(String[] args) { // Creating StringJoiner with delimeter "" StringJoiner str = new StringJoiner(", "); // Adding elements in the StringJoiner str.add("Geeks"); str.add("for"); str.add("Geeks"); str.add("A"); str.add("Computer"); str.add("Portal"); // Print the StringJoiner // using toString() method System.out.println("StringJoiner: " + str.toString()); } } |
StringJoiner: Geeks, for, Geeks, A, Computer, Portal
Recommended Posts:
- StringJoiner add() method in Java
- StringJoiner merge() method in Java
- StringJoiner length() method in Java
- StringJoiner setEmptyValue() method in Java
- Java.lang.Short toString() method in Java with Examples
- ArrayBlockingQueue toString() Method in Java
- Vector toString() method in Java with Example
- BigInteger toString() Method in Java
- HashSet toString() method in Java with Example
- Hashtable toString() Method in Java
- LinkedBlockingQueue toString() method in Java
- LinkedBlockingDeque toString() method in Java
- LinkedHashSet toString() method in Java with Example
- Object toString() Method in Java
- MathContext toString() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



