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.
Constructors :
- 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.
// Java program to demonstrate methods // of StringJoiner class import java.util.ArrayList; import java.util.StringJoiner; public class Test2 { public static void main(String[] args) { ArrayList<String> al = new ArrayList<>(); al.add( "Ram" ); al.add( "Shyam" ); al.add( "Alice" ); al.add( "Bob" ); StringJoiner sj1 = new StringJoiner( "," ); // setEmptyValue() method sj1.setEmptyValue( "sj1 is empty" ); System.out.println(sj1); // add() method sj1.add(al.get( 0 )).add(al.get( 1 )); System.out.println(sj1); // length() method System.out.println( "Length of sj1 : " + sj1.length()); StringJoiner sj2 = new StringJoiner( ":" ); sj2.add(al.get( 2 )).add(al.get( 3 )); //merge() method sj1.merge(sj2); // toString() method System.out.println(sj1.toString()); System.out.println( "Length of new sj1 : " + sj1.length()); } } |
Output:
sj1 is empty Ram,Shyam Length of sj1 : 9 Ram,Shyam,Alice:Bob Length of new sj1 : 19
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
leave a comment
0 Comments