Package com.google.common.base
Class Joiner
- java.lang.Object
-
- com.google.common.base.Joiner
-
@GwtCompatible public class Joiner extends java.lang.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 anAppendableor 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 throwNullPointerExceptionif any given element is null.Warning: joiner instances are always immutable; a configuration method such as
useForNullhas 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 finalconstants.// 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 classJoiner.MapJoinerAn object that joins map entries in the same manner asJoinerjoins iterables and arrays.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <A extends java.lang.Appendable>
AappendTo(A appendable, @Nullable java.lang.Object[] parts)Appends the string representation of each ofparts, using the previously configured separator between each, toappendable.<A extends java.lang.Appendable>
AappendTo(A appendable, java.lang.Iterable<? extends @Nullable java.lang.Object> parts)Appends the string representation of each ofparts, using the previously configured separator between each, toappendable.<A extends java.lang.Appendable>
AappendTo(A appendable, java.lang.Object first, java.lang.Object second, @Nullable java.lang.Object... rest)Appends toappendablethe string representation of each of the remaining arguments.<A extends java.lang.Appendable>
AappendTo(A appendable, java.util.Iterator<? extends @Nullable java.lang.Object> parts)Appends the string representation of each ofparts, using the previously configured separator between each, toappendable.java.lang.StringBuilderappendTo(java.lang.StringBuilder builder, @Nullable java.lang.Object[] parts)Appends the string representation of each ofparts, using the previously configured separator between each, tobuilder.java.lang.StringBuilderappendTo(java.lang.StringBuilder builder, java.lang.Iterable<? extends @Nullable java.lang.Object> parts)Appends the string representation of each ofparts, using the previously configured separator between each, tobuilder.java.lang.StringBuilderappendTo(java.lang.StringBuilder builder, java.lang.Object first, java.lang.Object second, @Nullable java.lang.Object... rest)Appends tobuilderthe string representation of each of the remaining arguments.java.lang.StringBuilderappendTo(java.lang.StringBuilder builder, java.util.Iterator<? extends @Nullable java.lang.Object> parts)Appends the string representation of each ofparts, using the previously configured separator between each, tobuilder.java.lang.Stringjoin(@Nullable java.lang.Object[] parts)Returns a string containing the string representation of each ofparts, using the previously configured separator between each.java.lang.Stringjoin(java.lang.Iterable<? extends @Nullable java.lang.Object> parts)Returns a string containing the string representation of each ofparts, using the previously configured separator between each.java.lang.Stringjoin(java.lang.Object first, java.lang.Object second, @Nullable java.lang.Object... rest)Returns a string containing the string representation of each argument, using the previously configured separator between each.java.lang.Stringjoin(java.util.Iterator<? extends @Nullable java.lang.Object> parts)Returns a string containing the string representation of each ofparts, using the previously configured separator between each.static Joineron(char separator)Returns a joiner which automatically placesseparatorbetween consecutive elements.static Joineron(java.lang.String separator)Returns a joiner which automatically placesseparatorbetween consecutive elements.JoinerskipNulls()Returns a joiner with the same behavior as this joiner, except automatically skipping over any provided null elements.JoineruseForNull(java.lang.String nullText)Returns a joiner with the same behavior as this one, except automatically substitutingnullTextfor any provided null elements.Joiner.MapJoinerwithKeyValueSeparator(char keyValueSeparator)Returns aMapJoinerusing the given key-value separator, and the same configuration as thisJoinerotherwise.Joiner.MapJoinerwithKeyValueSeparator(java.lang.String keyValueSeparator)Returns aMapJoinerusing the given key-value separator, and the same configuration as thisJoinerotherwise.
-
-
-
Method Detail
-
on
public static Joiner on(java.lang.String separator)
Returns a joiner which automatically placesseparatorbetween consecutive elements.
-
on
public static Joiner on(char separator)
Returns a joiner which automatically placesseparatorbetween consecutive elements.
-
appendTo
@CanIgnoreReturnValue public <A extends java.lang.Appendable> A appendTo(A appendable, java.lang.Iterable<? extends @Nullable java.lang.Object> parts) throws java.io.IOException
Appends the string representation of each ofparts, using the previously configured separator between each, toappendable.- Throws:
java.io.IOException
-
appendTo
@CanIgnoreReturnValue public <A extends java.lang.Appendable> A appendTo(A appendable, java.util.Iterator<? extends @Nullable java.lang.Object> parts) throws java.io.IOException
Appends the string representation of each ofparts, using the previously configured separator between each, toappendable.- Throws:
java.io.IOException- Since:
- 11.0
-
appendTo
@CanIgnoreReturnValue public final <A extends java.lang.Appendable> A appendTo(A appendable, @Nullable java.lang.Object[] parts) throws java.io.IOException
Appends the string representation of each ofparts, using the previously configured separator between each, toappendable.- Throws:
java.io.IOException
-
appendTo
@CanIgnoreReturnValue public final <A extends java.lang.Appendable> A appendTo(A appendable, @CheckForNull java.lang.Object first, @CheckForNull java.lang.Object second, @Nullable java.lang.Object... rest) throws java.io.IOException
Appends toappendablethe string representation of each of the remaining arguments.- Throws:
java.io.IOException
-
appendTo
@CanIgnoreReturnValue public final java.lang.StringBuilder appendTo(java.lang.StringBuilder builder, java.lang.Iterable<? extends @Nullable java.lang.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 java.lang.StringBuilder appendTo(java.lang.StringBuilder builder, java.util.Iterator<? extends @Nullable java.lang.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 java.lang.StringBuilder appendTo(java.lang.StringBuilder builder, @Nullable java.lang.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 java.lang.StringBuilder appendTo(java.lang.StringBuilder builder, @CheckForNull java.lang.Object first, @CheckForNull java.lang.Object second, @Nullable java.lang.Object... rest)
Appends tobuilderthe string representation of each of the remaining arguments. Identical toappendTo(Appendable, Object, Object, Object...), except that it does not throwIOException.
-
join
public final java.lang.String join(java.lang.Iterable<? extends @Nullable java.lang.Object> parts)
Returns a string containing the string representation of each ofparts, using the previously configured separator between each.
-
join
public final java.lang.String join(java.util.Iterator<? extends @Nullable java.lang.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 java.lang.String join(@Nullable java.lang.Object[] parts)
Returns a string containing the string representation of each ofparts, using the previously configured separator between each.
-
join
public final java.lang.String join(@CheckForNull java.lang.Object first, @CheckForNull java.lang.Object second, @Nullable java.lang.Object... rest)
Returns a string containing the string representation of each argument, using the previously configured separator between each.
-
useForNull
public Joiner useForNull(java.lang.String nullText)
Returns a joiner with the same behavior as this one, except automatically substitutingnullTextfor 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 aMapJoinerusing the given key-value separator, and the same configuration as thisJoinerotherwise.- Since:
- 20.0
-
withKeyValueSeparator
public Joiner.MapJoiner withKeyValueSeparator(java.lang.String keyValueSeparator)
Returns aMapJoinerusing the given key-value separator, and the same configuration as thisJoinerotherwise.
-
-