# StringJoiner

public final class **StringJoiner** extends [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)  
`StringJoiner` is used to construct a sequence of characters separated
by a delimiter and optionally starting with a supplied prefix
and ending with a supplied suffix.


Prior to adding something to the `StringJoiner`, its
`sj.toString()` method will, by default, return `prefix + suffix`.
However, if the `setEmptyValue` method is called, the `emptyValue`
supplied will be returned instead. This can be used, for example, when
creating a string using set notation to indicate an empty set, i.e.
`"{}"`, where the `prefix` is `"{"`, the
`suffix` is `"}"` and nothing has been added to the
`StringJoiner`.

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/stream/Collectors.html#joining(java.lang.CharSequence)`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/stream/Collectors.html#joining(java.lang.CharSequence,%20java.lang.CharSequence,%20java.lang.CharSequence)`

### Public Constructor Summary

|---|---|
|   | [StringJoiner](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/StringJoiner.html#StringJoiner(java.lang.CharSequence))([CharSequence](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/CharSequence.html) delimiter) Constructs a `StringJoiner` with no characters in it, with no `prefix` or `suffix`, and a copy of the supplied `delimiter`. |
|   | [StringJoiner](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/StringJoiner.html#StringJoiner(java.lang.CharSequence,%20java.lang.CharSequence,%20java.lang.CharSequence))([CharSequence](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/CharSequence.html) delimiter, [CharSequence](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/CharSequence.html) prefix, [CharSequence](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/CharSequence.html) suffix) Constructs a `StringJoiner` with no characters in it using copies of the supplied `prefix`, `delimiter` and `suffix`. |

### Public Method Summary

|---|---|
| [StringJoiner](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/StringJoiner.html) | [add](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/StringJoiner.html#add(java.lang.CharSequence))([CharSequence](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/CharSequence.html) newElement) Adds a copy of the given `CharSequence` value as the next element of the `StringJoiner` value. |
| int | [length](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/StringJoiner.html#length())() Returns the length of the `String` representation of this `StringJoiner`. |
| [StringJoiner](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/StringJoiner.html) | [merge](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/StringJoiner.html#merge(java.util.StringJoiner))([StringJoiner](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/StringJoiner.html) other) Adds the contents of the given `StringJoiner` without prefix and suffix as the next element if it is non-empty. |
| [StringJoiner](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/StringJoiner.html) | [setEmptyValue](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/StringJoiner.html#setEmptyValue(java.lang.CharSequence))([CharSequence](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/CharSequence.html) emptyValue) 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. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/StringJoiner.html#toString())() 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 |

### Inherited Method Summary

From class [java.lang.Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)

|---|---|
| [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |
| final [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<?\> | [getClass](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#getClass())() Returns the unique instance of `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html` that represents this object's class. |
| int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |
| final void | [notify](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| final void | [notifyAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |

## Public Constructors

#### public
**StringJoiner**
([CharSequence](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/CharSequence.html) delimiter)

Constructs a `StringJoiner` with no characters in it, with no
`prefix` or `suffix`, and a copy of the supplied
`delimiter`.
If no characters are added to the `StringJoiner` and methods
accessing the value of it are invoked, it will not return a
`prefix` or `suffix` (or properties thereof) in the result,
unless `setEmptyValue` has first been called.

##### Parameters

| delimiter | the sequence of characters to be used between each element added to the `StringJoiner` value |
|---|---|

##### Throws

| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if `delimiter` is `null` |
|---|---|

#### public
**StringJoiner**
([CharSequence](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/CharSequence.html) delimiter, [CharSequence](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/CharSequence.html) prefix, [CharSequence](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/CharSequence.html) 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.

##### Parameters

| delimiter | the sequence of characters to be used between each element added to the `StringJoiner` |
| prefix | the sequence of characters to be used at the beginning |
| suffix | the sequence of characters to be used at the end |
|---|---|

##### Throws

| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if `prefix`, `delimiter`, or `suffix` is `null` |
|---|---|

## Public Methods

#### public [StringJoiner](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/StringJoiner.html)
**add**
([CharSequence](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/CharSequence.html) newElement)

Adds a copy of the given `CharSequence` value as the next
element of the `StringJoiner` value. If `newElement` is
`null`, then `"null"` is added.

##### Parameters

| newElement | The element to add |
|---|---|

##### Returns

- a reference to this `StringJoiner`

#### public int
**length**
()

Returns the length of the `String` representation
of this `StringJoiner`. Note that if
no add methods have been called, then the length of the `String`
representation (either `prefix + suffix` or `emptyValue`)
will be returned. The value should be equivalent to
`toString().length()`.

##### Returns

- the length of the current value of `StringJoiner`

#### public [StringJoiner](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/StringJoiner.html)
**merge**
([StringJoiner](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/StringJoiner.html) other)

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.

A `StringJoiner` is empty if `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/StringJoiner.html#add(java.lang.CharSequence)`
has never been called, and if `merge()` has never been called
with a non-empty `StringJoiner` argument.

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.

##### Parameters

| other | The `StringJoiner` whose contents should be merged into this one |
|---|---|

##### Returns

- This `StringJoiner`

##### Throws

| [NullPointerException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if the other `StringJoiner` is null |
|---|---|

#### public [StringJoiner](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/StringJoiner.html)
**setEmptyValue**
([CharSequence](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/CharSequence.html) emptyValue)

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. A copy of the `emptyValue`
parameter is made for this purpose. Note that once an add method has been
called, the `StringJoiner` is no longer considered empty, even if
the element(s) added correspond to the empty `String`.

##### 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](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | when the `emptyValue` parameter is `null` |
|---|---|

#### public [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html)
**toString**
()

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

##### Returns

- the string representation of this `StringJoiner`