Class Joiner


  • @GwtCompatible
    public class Joiner
    extends 
    An object which joins pieces of text (specified as an array, , varargs or even a ) with a separator. It either appends the results to an or returns them as a . 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 before being appended.

    If neither skipNulls() nor useForNull(String) is specified, the joining methods will throw 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 .

    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 as Joiner joins iterables and arrays.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <A extends >
      A
      appendTo​(A appendable,  first,  second, ... rest)
      Appends to appendable the string representation of each of the remaining arguments.
      <A extends >
      A
      appendTo​(A appendable, <?> parts)
      Appends the string representation of each of parts, using the previously configured separator between each, to appendable.
      <A extends >
      A
      appendTo​(A appendable, [] parts)
      Appends the string representation of each of parts, using the previously configured separator between each, to appendable.
      <A extends >
      A
      appendTo​(A appendable, <?> parts)
      Appends the string representation of each of parts, using the previously configured separator between each, to appendable.
       builder,  first,  second, ... rest)
      Appends to builder the string representation of each of the remaining arguments.
       builder, <?> parts)
      Appends the string representation of each of parts, using the previously configured separator between each, to builder.
       builder, [] parts)
      Appends the string representation of each of parts, using the previously configured separator between each, to builder.
       builder, <?> parts)
      Appends the string representation of each of parts, using the previously configured separator between each, to builder.
       first,  second, ... rest)
      Returns a string containing the string representation of each argument, using the previously configured separator between each.
      <?> parts)
      Returns a string containing the string representation of each of parts, using the previously configured separator between each.
      [] parts)
      Returns a string containing the string representation of each of parts, using the previously configured separator between each.
      <?> parts)
      Returns a string containing the string representation of each of parts, using the previously configured separator between each.
      static Joiner on​(char separator)
      Returns a joiner which automatically places separator between consecutive elements.
      static Joiner  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  nullText)
      Returns a joiner with the same behavior as this one, except automatically substituting nullText for any provided null elements.
      Joiner.MapJoiner withKeyValueSeparator​(char keyValueSeparator)
      Returns a MapJoiner using the given key-value separator, and the same configuration as this Joiner otherwise.
      Joiner.MapJoiner  keyValueSeparator)
      Returns a MapJoiner using the given key-value separator, and the same configuration as this Joiner otherwise.
      • Methods inherited from class java.lang.

        , , , , , , , , , ,
    • Method Detail

      • on

        public static  separator)
        Returns a joiner which automatically places separator between consecutive elements.
      • on

        public static Joiner on​(char separator)
        Returns a joiner which automatically places separator between consecutive elements.
      • appendTo

        public <A extends > A appendTo​(A appendable,
                                                 <?> parts)
                                          throws 
        Appends the string representation of each of parts, using the previously configured separator between each, to appendable.
        Throws:
      • appendTo

        public <A extends > A appendTo​(A appendable,
                                                 <?> parts)
                                          throws 
        Appends the string representation of each of parts, using the previously configured separator between each, to appendable.
        Throws:
        Since:
        11.0
      • appendTo

        public final <A extends > A appendTo​(A appendable,
                                                       [] parts)
                                                throws 
        Appends the string representation of each of parts, using the previously configured separator between each, to appendable.
        Throws:
      • appendTo

        public final <A extends > A appendTo​(A appendable,
                                                         first,
                                                         second,
                                                       ... rest)
                                                throws 
        Appends to appendable the string representation of each of the remaining arguments.
        Throws:
      • appendTo

        public final   builder,
                                            <?> parts)
        Appends the string representation of each of parts, using the previously configured separator between each, to builder. Identical to appendTo(Appendable, Iterable), except that it does not throw .
      • appendTo

        public final   builder,
                                            <?> parts)
        Appends the string representation of each of parts, using the previously configured separator between each, to builder. Identical to appendTo(Appendable, Iterable), except that it does not throw .
        Since:
        11.0
      • appendTo

        public final   builder,
                                            [] parts)
        Appends the string representation of each of parts, using the previously configured separator between each, to builder. Identical to appendTo(Appendable, Iterable), except that it does not throw .
      • appendTo

        public final   builder,
                                              first,
                                              second,
                                            ... rest)
        Appends to builder the string representation of each of the remaining arguments. Identical to appendTo(Appendable, Object, Object, Object...), except that it does not throw .
      • join

        public final  <?> parts)
        Returns a string containing the string representation of each of parts, using the previously configured separator between each.
      • join

        public final  <?> parts)
        Returns a string containing the string representation of each of parts, using the previously configured separator between each.
        Since:
        11.0
      • join

        public final  [] parts)
        Returns a string containing the string representation of each of parts, using the previously configured separator between each.
      • join

        public final    first,
                                   second,
                                 ... rest)
        Returns a string containing the string representation of each argument, using the previously configured separator between each.
      • useForNull

        public  nullText)
        Returns a joiner with the same behavior as this one, except automatically substituting nullText 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 a MapJoiner using the given key-value separator, and the same configuration as this Joiner otherwise.
        Since:
        20.0
      • withKeyValueSeparator

        public  keyValueSeparator)
        Returns a MapJoiner using the given key-value separator, and the same configuration as this Joiner otherwise.