StringJoiner add() method in Java
The add(CharSequence newElement) of StringJoiner adds a copy of the given CharSequence value as the next element of the StringJoiner value. If newElement is null, then “null” is added.
Syntax:
public StringJoiner add(CharSequence newElement)
Parameters: This method takes a mandatory parameter newElement which is the element to be added.
Returns: This method returns a reference to this StringJoiner
Below programs illustrate the add() method:
Example 1: To demonstrate add() with delimeter ” ”
// Java program to demonstrate // add() method of StringJoiner import java.util.StringJoiner; public class GFG1 { 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 System.out.println(str.toString()); } } |
Geeks for Geeks
Example 2: To demonstrate add() with delimeter “,”
// Java program to demonstrate // add() method of StringJoiner import java.util.StringJoiner; public class GFG1 { 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 System.out.println(str.toString()); } } |
Geeks,for,Geeks
Recommended Posts:
- StringJoiner setEmptyValue() method in Java
- StringJoiner toString() method in Java
- StringJoiner merge() method in Java
- StringJoiner length() method in Java
- Java.util.StringJoiner in Java8
- When to use StringJoiner over StringBuilder?
- Java.util.Collections.rotate() Method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
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.



