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 delimiter ” ”
Java
import java.util.StringJoiner;
public class GFG {
public static void main(String[] args)
{
StringJoiner str = new StringJoiner(" ");
str.add("Geeks");
str.add(" for ");
str.add("Geeks");
System.out.println("StringJoiner: "
+ str.toString());
}
}
|
Output:
StringJoiner: Geeks for Geeks
Example 2: To demonstrate toString() with delimiter “, “
Java
import java.util.StringJoiner;
public class GFG {
public static void main(String[] args)
{
StringJoiner str = new StringJoiner(", ");
str.add("Geeks");
str.add(" for ");
str.add("Geeks");
str.add("A");
str.add("Computer");
str.add("Portal");
System.out.println("StringJoiner: "
+ str.toString());
}
}
|
Output:
StringJoiner: Geeks, for, Geeks, A, Computer, Portal
Last Updated :
12 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...