--- old/src/share/classes/java/lang/String.java 2009-02-06 18:03:25.000000000 +0100 +++ new/src/share/classes/java/lang/String.java 2009-02-06 18:03:25.000000000 +0100 @@ -2826,6 +2826,96 @@ } /** + * Returns a new String that is a composition of the values returned + * by calling {@link String#valueOf(Object)} on each element and joining them together + * with the current String instance. + * + *
An example: + *+ * + * @param first the first element to join + * @param elements the other Object elements to join together with the first one. + * @return a new String that is composed from the first and the elements parameters and + * the String instance used as delimiter. + * + * @see StringBuilder#join(String,Object,Object[]) + * + * @throws NullPointerException + * If elements is null + * + * @since 1.7 + */ + public String join(Object first, Object... elements) { + if (elements.length==0) + return String.valueOf(first); + return new StringBuilder().join(this, first, elements).toString(); + } + + /** + * Returns a new String that is a composition of the values returned + * by calling {@link String#valueOf(Object)} on each array element + * and joining them together with the current String instance. + * + *+ * String message = " ".join("Java", "is", "cool" ); + * // message returned is: "Java is cool" + *
An example: + *+ * + * @param elements an array of Object element to join together. + * @return a new String that is composed from the elements parameters joining + * with the current String instance. + * + * @see #join(Object[]) + * @see StringBuilder#join(String,Object[]) + * + * @throws NullPointerException + * If elements is null + * + * @since 1.7 + */ + public String join(Object[] elements) { + return new StringBuilder().join(this, elements).toString(); + } + + /** + * Returns a new String that is a composition of the values returned by + * calling {@link String#valueOf(Object)} on each of the elements + * parameter and joining them together with the current String instance. + * + *+ * String[] strings = new String[]{ "Java", "is", "cool" }; + * String message = " ".join( strings ); + * // message returned is: "Java is cool" + *
Two examples: + *+ * + * @param elements an Iterable that will have its elements joined together. + * @return a new String that is composed from the elements parameters + * and the current String instance. + * + * @see #join(Object[]) + * @see StringBuilder#join(String,Iterable) + * + * @throws NullPointerException + * If elements is null + * + * @since 1.7 + */ + public String join(Iterable> elements) { + return new StringBuilder().join(this, elements).toString(); + } + + /** * Returns the string representation of the+ * List<String> strings = new LinkedList<String>(); + * strings.add( "Java" ); strings.add( "is" ); strings.add( "cool" ); + * String message = " ".join( strings ); + * //message returned is: "Java is cool" + * Set<String> strings = new HashSet<String>(); + * strings.add( "Java" ); strings.add( "is" ); strings.add( "cool" ); + * String message = " ".join( strings ); + * //message returned is: "Java is cool" + *
Object
argument.
*
* @param obj an Object
.