Java StringJoinerJava added a new final class StringJoiner in java.util package. It is used to construct a sequence of characters separated by a delimiter. Now, you can create string by passing delimiters like comma(,), hyphen(-) etc. You can also pass prefix and suffix to the char sequence. StringJoiner ConstructorsConstructor | Description |
---|
Public StringJoiner(CharSequence delimiter) | It constructs a StringJoiner with no characters in it, with no prefix or suffix, and a copy of the supplied delimiter. It throws NullPointerException if delimiter is null. | Public StringJoiner(CharSequence delimiter,CharSequence prefix,CharSequence suffix) | It constructs a StringJoiner with no characters in it using copies of the supplied prefix, delimiter and suffix. It throws NullPointerException if prefix, delimiter, or suffix is null. |
StringJoiner MethodsMethod | Description |
---|
Public StringJoiner add(CharSequence newElement) | It adds a copy of the given CharSequence value as the next element of the StringJoiner value. If newElement is null,"null" is added. | Public StringJoiner merge(StringJoiner other) | It 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. | Public int length() | It returns the length of the String representation of this StringJoiner. | Public StringJoiner setEmptyValue(CharSequence emptyValue) | It sets the sequence of characters to be used when determining the string representation of this StringJoiner and no elements have been added yet, that is, when it is empty. |
Java StringJoiner ExampleOutput:
Java StringJoiner Example: adding prefix and suffixOutput: [Rahul,Raju,Peter,Raheem]
StringJoiner Example: Merge Two StringJoinerThe merge() method merges two StringJoiner objects excluding of prefix and suffix of second StringJoiner object. Output: [Rahul,Raju,Peter:Raheem]
StringJoiner Example: StringJoiner MethodsOutput:
It is empty
Rahul,Raju
Length: 10
Rahul,Raju
Character at index 3: u
Rahul,Raju,Sorabh
New Length: 17
|