@GwtCompatible public class Joiner extends 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 before being appended.
If neither skipNulls() nor useForNull(String) is specified, the joining methods will throw NullPointerException if any given element is null.
Warning: joiner instances are always immutable; a configuration method such as useForNull has no effect on the instance it is invoked on! You must store and use the new joiner instance returned by the method. This makes joiners thread-safe, and safe to store as static final constants.
// Bad! Do not do this! Joiner joiner = Joiner.on(','); joiner.skipNulls(); // does nothing! return joiner.join("wrong", null, "wrong");
See the Guava User Guide article on Joiner.
| Modifier and Type | Class and Description |
|---|---|
static class |
Joiner
An object that joins map entries in the same manner as
Joiner joins iterables and arrays.
|
| Modifier and Type | Method and Description |
|---|---|
<A extends Appendable |
appendTo(A appendable, Iterable
Appends the string representation of each of
parts, using the previously configured separator between each, to
appendable.
|
<A extends Appendable |
appendTo(A appendable, Iterator
Appends the string representation of each of
parts, using the previously configured separator between each, to
appendable.
|
<A extends Appendable |
appendTo(A appendable, Object
Appends the string representation of each of
parts, using the previously configured separator between each, to
appendable.
|
<A extends Appendable |
appendTo(A appendable, Object
Appends to
appendable the string representation of each of the remaining arguments.
|
StringBuilder |
appendTo(StringBuilder
Appends the string representation of each of
parts, using the previously configured separator between each, to
builder.
|
StringBuilder |
appendTo(StringBuilder
Appends the string representation of each of
parts, using the previously configured separator between each, to
builder.
|
StringBuilder |
appendTo(StringBuilder
Appends the string representation of each of
parts, using the previously configured separator between each, to
builder.
|
StringBuilder |
appendTo(StringBuilder
Appends to
builder the string representation of each of the remaining arguments.
|
String |
join(Iterable
Returns a string containing the string representation of each of
parts, using the previously configured separator between each.
|
String |
join(Iterator
Returns a string containing the string representation of each of
parts, using the previously configured separator between each.
|
String |
join(Object
Returns a string containing the string representation of each of
parts, using the previously configured separator between each.
|
String |
join(Object
Returns a string containing the string representation of each argument, using the previously configured separator between each.
|
static Joiner |
on(char separator)
Returns a joiner which automatically places
separator between consecutive elements.
|
static Joiner |
on(String
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(String
Returns a joiner with the same behavior as this one, except automatically substituting
nullText for any provided null elements.
|
Joiner |
withKeyValueSeparator(String
Returns a
MapJoiner using the given key-value separator, and the same configuration as this
Joiner otherwise.
|
public static Joineron(String separator)
separator between consecutive elements.
public static Joineron(char separator)
separator between consecutive elements.
public <A extends Appendable> A appendTo(A appendable, Iterable <?> parts) throws IOException
parts, using the previously configured separator between each, to
appendable.
IOException
public <A extends Appendable> A appendTo(A appendable, Iterator <?> parts) throws IOException
parts, using the previously configured separator between each, to
appendable.
IOException
public final <A extends Appendable> A appendTo(A appendable, Object [] parts) throws IOException
parts, using the previously configured separator between each, to
appendable.
IOException
public final <A extends Appendable> A appendTo(A appendable, Object first, Object second, Object ... rest) throws IOException
appendable the string representation of each of the remaining arguments.
IOException
public final StringBuilderappendTo(StringBuilder builder, 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 StringBuilderappendTo(StringBuilder builder, Iterator <?> 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 StringBuilderappendTo(StringBuilder builder, 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 StringBuilderappendTo(StringBuilder builder, Object first, Object second, 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 Stringjoin(Iterable <?> parts)
parts, using the previously configured separator between each.
public final Stringjoin(Iterator <?> parts)
parts, using the previously configured separator between each.
public final Stringjoin(Object [] parts)
parts, using the previously configured separator between each.
public final Stringjoin(Object first, Object second, Object ... rest)
public JoineruseForNull(String nullText)
nullText for any provided null elements.
public JoinerskipNulls()
public Joiner.MapJoiner withKeyValueSeparator(String keyValueSeparator)
MapJoiner using the given key-value separator, and the same configuration as this
Joiner otherwise.