Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/java/util/StringJoiner#pubctors) \| [Methods](https://developer.android.com/reference/java/util/StringJoiner#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/java/util/StringJoiner#inhmethods)

# StringJoiner


`
public

final

class
StringJoiner
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | java.util.StringJoiner |

<br />

*** ** * ** ***

`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://developer.android.com/reference/java/util/stream/Collectors#joining(java.lang.CharSequence)`
- `https://developer.android.com/reference/java/util/stream/Collectors#joining(java.lang.CharSequence,%20java.lang.CharSequence,%20java.lang.CharSequence)`

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/java/util/StringJoiner#StringJoiner(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence delimiter) ` Constructs a `StringJoiner` with no characters in it, with no `prefix` or `suffix`, and a copy of the supplied `delimiter`. |
| ` https://developer.android.com/reference/java/util/StringJoiner#StringJoiner(java.lang.CharSequence,%20java.lang.CharSequence,%20java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence delimiter, https://developer.android.com/reference/java/lang/CharSequence prefix, https://developer.android.com/reference/java/lang/CharSequence suffix) ` Constructs a `StringJoiner` with no characters in it using copies of the supplied `prefix`, `delimiter` and `suffix`. |

| ### Public methods ||
|---|---|
| ` https://developer.android.com/reference/java/util/StringJoiner` | ` https://developer.android.com/reference/java/util/StringJoiner#add(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence newElement) ` Adds a copy of the given `CharSequence` value as the next element of the `StringJoiner` value. |
| ` int` | ` https://developer.android.com/reference/java/util/StringJoiner#length()() ` Returns the length of the `String` representation of this `StringJoiner`. |
| ` https://developer.android.com/reference/java/util/StringJoiner` | ` https://developer.android.com/reference/java/util/StringJoiner#merge(java.util.StringJoiner)(https://developer.android.com/reference/java/util/StringJoiner other) ` Adds the contents of the given `StringJoiner` without prefix and suffix as the next element if it is non-empty. |
| ` https://developer.android.com/reference/java/util/StringJoiner` | ` https://developer.android.com/reference/java/util/StringJoiner#setEmptyValue(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence 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. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/util/StringJoiner#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 methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||

## Public constructors

### StringJoiner

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public StringJoiner (CharSequence 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.

<br />

| Parameters ||
|---|---|
| `delimiter` | `CharSequence`: the sequence of characters to be used between each element added to the `StringJoiner` value <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if `delimiter` is `null` |

### StringJoiner

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public 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.

<br />

| Parameters ||
|---|---|
| `delimiter` | `CharSequence`: the sequence of characters to be used between each element added to the `StringJoiner` <br /> |
| `prefix` | `CharSequence`: the sequence of characters to be used at the beginning <br /> |
| `suffix` | `CharSequence`: the sequence of characters to be used at the end <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if `prefix`, `delimiter`, or `suffix` is `null` |

## Public methods

### add

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public StringJoiner add (CharSequence 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.

<br />

| Parameters ||
|---|---|
| `newElement` | `CharSequence`: The element to add <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/StringJoiner` | a reference to this `StringJoiner` <br /> |

### length

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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()`.

<br />

| Returns ||
|---|---|
| `int` | the length of the current value of `StringJoiner` <br /> |

### merge

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public StringJoiner merge (StringJoiner 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://developer.android.com/reference/java/util/StringJoiner#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.

<br />

| Parameters ||
|---|---|
| `other` | `StringJoiner`: The `StringJoiner` whose contents should be merged into this one <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/StringJoiner` | This `StringJoiner` <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if the other `StringJoiner` is null |

### setEmptyValue

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public StringJoiner setEmptyValue (CharSequence 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`.

<br />

| Parameters ||
|---|---|
| `emptyValue` | `CharSequence`: the characters to return as the value of an empty `StringJoiner` <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/StringJoiner` | this `StringJoiner` itself so the calls may be chained <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | when the `emptyValue` parameter is `null` |

### toString

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public String 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.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | the string representation of this `StringJoiner` <br /> |