Package com.google.common.base
Class Joiner
- java.lang.Object
-
- com.google.common.base.Joiner
-
@GwtCompatible public class Joiner extends Object
An object which joins pieces of text (specified as an array,Iterable
, varargs or even aMap
) with a separator. It either appends the results to anAppendable
or returns them as aString
. 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 usingObject.toString()
before being appended.If neither
skipNulls()
noruseForNull(String)
is specified, the joining methods will throwNullPointerException
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 asstatic 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
.- Since:
- 2.0
- Author:
- Kevin Bourrillion
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Joiner.MapJoiner
An object that joins map entries in the same manner asJoiner
joins iterables and arrays.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <A extends Appendable>
AappendTo(A appendable, @Nullable Object[] parts)
Appends the string representation of each ofparts
, using the previously configured separator between each, toappendable
.<A extends Appendable>
AappendTo(A appendable, Iterable<? extends @Nullable Object> parts)
Appends the string representation of each ofparts
, using the previously configured separator between each, toappendable
.<A extends Appendable>
AappendTo(A appendable, Object first, Object second, @Nullable Object... rest)
Appends toappendable
the string representation of each of the remaining arguments.<A extends Appendable>
AappendTo(A appendable, Iterator<? extends @Nullable Object> parts)
Appends the string representation of each ofparts
, using the previously configured separator between each, toappendable
.StringBuilder
appendTo(StringBuilder builder, @Nullable Object[] parts)
Appends the string representation of each ofparts
, using the previously configured separator between each, tobuilder
.StringBuilder
appendTo(StringBuilder builder, Iterable<? extends @Nullable Object> parts)
Appends the string representation of each ofparts
, using the previously configured separator between each, tobuilder
.StringBuilder
appendTo(StringBuilder builder, Object first, Object second, @Nullable Object... rest)
Appends tobuilder
the string representation of each of the remaining arguments.StringBuilder
appendTo(StringBuilder builder, Iterator<? extends @Nullable Object> parts)
Appends the string representation of each ofparts
, using the previously configured separator between each, tobuilder
.String
join(@Nullable Object[] parts)
Returns a string containing the string representation of each ofparts
, using the previously configured separator between each.String
join(Iterable<? extends @Nullable Object> parts)
Returns a string containing the string representation of each ofparts
, using the previously configured separator between each.String
join(Object first, Object second, @Nullable Object... rest)
Returns a string containing the string representation of each argument, using the previously configured separator between each.String
join(Iterator<? extends @Nullable Object> parts)
Returns a string containing the string representation of each ofparts
, using the previously configured separator between each.static Joiner
on(char separator)
Returns a joiner which automatically placesseparator
between consecutive elements.static Joiner
on(String separator)
Returns a joiner which automatically placesseparator
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 nullText)
Returns a joiner with the same behavior as this one, except automatically substitutingnullText
for any provided null elements.Joiner.MapJoiner
withKeyValueSeparator(char keyValueSeparator)
Returns aMapJoiner
using the given key-value separator, and the same configuration as thisJoiner
otherwise.Joiner.MapJoiner
withKeyValueSeparator(String keyValueSeparator)
Returns aMapJoiner
using the given key-value separator, and the same configuration as thisJoiner
otherwise.
-
-
-
Method Detail
-
on
public static Joiner on(String separator)
Returns a joiner which automatically placesseparator
between consecutive elements.
-
on
public static Joiner on(char separator)
Returns a joiner which automatically placesseparator
between consecutive elements.
-
appendTo
@CanIgnoreReturnValue public <A extends Appendable> A appendTo(A appendable, Iterable<? extends @Nullable Object> parts) throws IOException
Appends the string representation of each ofparts
, using the previously configured separator between each, toappendable
.- Throws:
IOException
-
appendTo
@CanIgnoreReturnValue public <A extends Appendable> A appendTo(A appendable, Iterator<? extends @Nullable Object> parts) throws IOException
Appends the string representation of each ofparts
, using the previously configured separator between each, toappendable
.- Throws:
IOException
- Since:
- 11.0
-
appendTo
@CanIgnoreReturnValue public final <A extends Appendable> A appendTo(A appendable, @Nullable Object[] parts) throws IOException
Appends the string representation of each ofparts
, using the previously configured separator between each, toappendable
.- Throws:
IOException
-
appendTo
@CanIgnoreReturnValue public final <A extends Appendable> A appendTo(A appendable, @CheckForNull Object first, @CheckForNull Object second, @Nullable Object... rest) throws IOException
Appends toappendable
the string representation of each of the remaining arguments.- Throws:
IOException
-
appendTo
@CanIgnoreReturnValue public final StringBuilder appendTo(StringBuilder builder, Iterable<? extends @Nullable Object> parts)
Appends the string representation of each ofparts
, using the previously configured separator between each, tobuilder
. Identical toappendTo(Appendable, Iterable)
, except that it does not throwIOException
.
-
appendTo
@CanIgnoreReturnValue public final StringBuilder appendTo(StringBuilder builder, Iterator<? extends @Nullable Object> parts)
Appends the string representation of each ofparts
, using the previously configured separator between each, tobuilder
. Identical toappendTo(Appendable, Iterable)
, except that it does not throwIOException
.- Since:
- 11.0
-
appendTo
@CanIgnoreReturnValue public final StringBuilder appendTo(StringBuilder builder, @Nullable Object[] parts)
Appends the string representation of each ofparts
, using the previously configured separator between each, tobuilder
. Identical toappendTo(Appendable, Iterable)
, except that it does not throwIOException
.
-
appendTo
@CanIgnoreReturnValue public final StringBuilder appendTo(StringBuilder builder, @CheckForNull Object first, @CheckForNull Object second, @Nullable Object... rest)
Appends tobuilder
the string representation of each of the remaining arguments. Identical toappendTo(Appendable, Object, Object, Object...)
, except that it does not throwIOException
.
-
join
public String join(Iterable<? extends @Nullable Object> parts)
Returns a string containing the string representation of each ofparts
, using the previously configured separator between each.
-
join
public final String join(Iterator<? extends @Nullable Object> parts)
Returns a string containing the string representation of each ofparts
, using the previously configured separator between each.- Since:
- 11.0
-
join
public final String join(@Nullable Object[] parts)
Returns a string containing the string representation of each ofparts
, using the previously configured separator between each.
-
join
public final String join(@CheckForNull Object first, @CheckForNull Object second, @Nullable Object... rest)
Returns a string containing the string representation of each argument, using the previously configured separator between each.
-
useForNull
public Joiner useForNull(String nullText)
Returns a joiner with the same behavior as this one, except automatically substitutingnullText
for any provided null elements.
-
skipNulls
public Joiner skipNulls()
Returns a joiner with the same behavior as this joiner, except automatically skipping over any provided null elements.
-
withKeyValueSeparator
public Joiner.MapJoiner withKeyValueSeparator(char keyValueSeparator)
Returns aMapJoiner
using the given key-value separator, and the same configuration as thisJoiner
otherwise.- Since:
- 20.0
-
withKeyValueSeparator
public Joiner.MapJoiner withKeyValueSeparator(String keyValueSeparator)
Returns aMapJoiner
using the given key-value separator, and the same configuration as thisJoiner
otherwise.
-
-