StringJoiner is a class in java.util package which is used to construct a sequence of characters(strings) separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix. Though this can also be done with the help of StringBuilder class to append delimiter after each string, but StringJoiner provides easy way to do that without much code to write.
- StringJoiner(CharSequence delimiter) : Constructs a StringJoiner with no characters in it, with no prefix or suffix, and a copy of the supplied delimiter.
Syntax : public StringJoiner(CharSequence delimiter) Parameters : delimiter - the sequence of characters to be used between each element added to the StringJoiner value Throws: NullPointerException - if delimiter is null
- StringJoiner(CharSequence delimiter,CharSequence prefix,CharSequence suffix) : Constructs a StringJoiner with no characters in it using copies of the supplied prefix, delimiter and suffix. If no characters are added to the StringJoiner and methods accessing the string value of it are invoked, it will return the prefix + suffix (or properties thereof) in the result, unless setEmptyValue has first been called.
Syntax : public StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix) Parameters : delimiter - the sequence of characters to be used between each element added to the StringJoiner value prefix - the sequence of characters to be used at the beginning suffix - the sequence of characters to be used at the end Throws: NullPointerException - if prefix, delimiter, or suffix is null
Methods : There are 5 methods in StringJoiner class.
- String toString() : This method returns String object of this StringJoiner.
Syntax : public String toString() Parameters : NA Returns : the string representation of this StringJoiner Overrides : toString in class Object
- StringJoiner add(CharSequence newElement) : This method 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 : newElement - The element to add Returns : a reference to this StringJoiner
- StringJoiner merge(StringJoiner other) : This method adds the contents of the given StringJoiner without prefix and suffix as the next element if it is non-empty. If the given StringJoiner is empty, the call has no effect.
If the other StringJoiner is using a different delimiter, then elements from the other StringJoiner are concatenated with that delimiter and the result is appended to this StringJoiner as a single element.
Syntax : public StringJoiner merge(StringJoiner other) Parameters : other - The StringJoiner whose contents should be merged into this one Returns : This StringJoiner Throws : NullPointerException - if the other StringJoiner is null
- int length() : This method returns the length of the String representation of this StringJoiner.
Syntax : public int length() Parameters : NA Returns : This StringJoiner
- StringJoiner setEmptyValue(CharSequence emptyValue) : This method sets string to be used when determining the string representation of this StringJoiner and no elements have been added yet, that is, when it is empty.
Syntax : public StringJoiner setEmptyValue(CharSequence emptyValue) Parameters : emptyValue - the characters to return as the value of an empty StringJoiner Returns : this StringJoiner itself so the calls may be chained Throws: NullPointerException - when the emptyValue parameter is null
Below is the java program to demonstrate all methods.
sj1 is empty Ram,Shyam Length of sj1 : 9 Ram,Shyam,Alice:Bob Length of new sj1 : 19
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.