public class Joiner
extends java.lang.Object
Iterable
, varargs or even a Map
) with a separator. It either
appends the results to an Appendable
or returns them as a String
. Example: Joiner joiner = Joiner.on("; ").skipNulls();
. . .
return joiner.join("Harry", null, "Ron", "Hermione");
This returns the string "Harry; Ron; Hermione"
. Note that all input
elements are converted to strings using Object.toString()
before
being appended.
If neither skipNulls()
nor useForNull(String)
is
specified, the joining methods will throw NullPointerException
if any
given element is null.
Modifier and Type | Class and Description |
---|---|
static class |
Joiner.MapJoiner
An object that joins map entries in the same manner as
Joiner joins
iterables and arrays. |
Modifier and Type | Method and Description |
---|---|
<A extends java.lang.Appendable> |
appendTo(A appendable,
java.lang.Iterable<?> parts)
Appends the string representation of each of
parts , using the
previously configured separator between each, to appendable . |
<A extends java.lang.Appendable> |
appendTo(A appendable,
java.lang.Object[] parts)
Appends the string representation of each of
parts , using the
previously configured separator between each, to appendable . |
<A extends java.lang.Appendable> |
appendTo(A appendable,
java.lang.Object first,
java.lang.Object second,
java.lang.Object... rest)
Appends to
appendable the string representation of each of the
remaining arguments. |
java.lang.StringBuilder |
appendTo(java.lang.StringBuilder builder,
java.lang.Iterable<?> parts)
Appends the string representation of each of
parts , using the
previously configured separator between each, to builder . |
java.lang.StringBuilder |
appendTo(java.lang.StringBuilder builder,
java.lang.Object[] parts)
Appends the string representation of each of
parts , using the
previously configured separator between each, to builder . |
java.lang.StringBuilder |
appendTo(java.lang.StringBuilder builder,
java.lang.Object first,
java.lang.Object second,
java.lang.Object... rest)
Appends to
builder the string representation of each of the
remaining arguments. |
java.lang.String |
join(java.lang.Iterable<?> parts)
Returns a string containing the string representation of each of
parts , using the previously configured separator between each. |
java.lang.String |
join(java.lang.Object[] parts)
Returns a string containing the string representation of each of
parts , using the previously configured separator between each. |
java.lang.String |
join(java.lang.Object first,
java.lang.Object second,
java.lang.Object... rest)
Returns a string containing the string representation of each argument,
using the previously configured separator between each.
|
static Joiner |
on(java.lang.String separator)
Returns a joiner which automatically places
separator between
consecutive elements. |
Joiner |
skipNulls()
Returns a joiner with the same behavior as this joiner, except
automatically skipping over any provided null elements.
|
Joiner |
useForNull(java.lang.String nullText)
Returns a joiner with the same behavior as this one, except automatically
substituting
nullText for any provided null elements. |
Joiner.MapJoiner |
withKeyValueSeparator(java.lang.String keyValueSeparator)
Returns a
MapJoiner using the given key-value separator, and the
same configuration as this Joiner otherwise. |
public static Joiner on(java.lang.String separator)
separator
between
consecutive elements.public <A extends java.lang.Appendable> A appendTo(A appendable, java.lang.Iterable<?> parts) throws java.io.IOException
parts
, using the
previously configured separator between each, to appendable
.java.io.IOException
public final <A extends java.lang.Appendable> A appendTo(A appendable, java.lang.Object[] parts) throws java.io.IOException
parts
, using the
previously configured separator between each, to appendable
.java.io.IOException
public final <A extends java.lang.Appendable> A appendTo(A appendable, java.lang.Object first, java.lang.Object second, java.lang.Object... rest) throws java.io.IOException
appendable
the string representation of each of the
remaining arguments.java.io.IOException
public final java.lang.StringBuilder appendTo(java.lang.StringBuilder builder, java.lang.Iterable<?> parts)
parts
, using the
previously configured separator between each, to builder
. Identical
to appendTo(Appendable, Iterable)
, except that it does not throw
IOException
.public final java.lang.StringBuilder appendTo(java.lang.StringBuilder builder, java.lang.Object[] parts)
parts
, using the
previously configured separator between each, to builder
. Identical
to appendTo(Appendable, Iterable)
, except that it does not throw
IOException
.public final java.lang.StringBuilder appendTo(java.lang.StringBuilder builder, java.lang.Object first, java.lang.Object second, java.lang.Object... rest)
builder
the string representation of each of the
remaining arguments. Identical to appendTo(Appendable, Object,
Object, Object[])
, except that it does not throw IOException
.public final java.lang.String join(java.lang.Iterable<?> parts)
parts
, using the previously configured separator between each.public final java.lang.String join(java.lang.Object[] parts)
parts
, using the previously configured separator between each.public final java.lang.String join(java.lang.Object first, java.lang.Object second, java.lang.Object... rest)
public Joiner useForNull(java.lang.String nullText)
nullText
for any provided null elements.public Joiner skipNulls()
public Joiner.MapJoiner withKeyValueSeparator(java.lang.String keyValueSeparator)
MapJoiner
using the given key-value separator, and the
same configuration as this Joiner
otherwise.