/*** Returns a new String composed of copies of the

* {@codeCharSequence elements} joined together with a copy of

* the specified {@codedelimiter}.

* //这是用法示例

*

For example,

*

{@code*     String message = String.join("-", "Java", "is", "cool");

* // message returned is: "Java-is-cool"

* }

*

* Note that if an element is null, then {@code"null"} is added.

*

*@paramdelimiter the delimiter that separates each element

*@paramelements the elements to join together.

*

*@returna new {@codeString} that is composed of the {@codeelements}

* separated by the {@codedelimiter}

*

*@throwsNullPointerException If {@codedelimiter} or {@codeelements}

* is {@codenull}

*

*@seejava.util.StringJoiner

*@since1.8*/

public staticString join(CharSequence delimiter, CharSequence... elements) {

Objects.requireNonNull(delimiter);

Objects.requireNonNull(elements);//Number of elements not likely worth Arrays.stream overhead.

StringJoiner joiner = newStringJoiner(delimiter);for(CharSequence cs: elements) {

joiner.add(cs);

}returnjoiner.toString();

}/*** Returns a new {@codeString} composed of copies of the

* {@codeCharSequence elements} joined together with a copy of the

* specified {@codedelimiter}.

*

*

For example,

*

{@code*     List strings = new LinkedList<>();

* strings.add("Java");strings.add("is");

* strings.add("cool");

* String message = String.join(" ", strings);

* //message returned is: "Java is cool"

*

* Set strings = new LinkedHashSet<>();

* strings.add("Java"); strings.add("is");

* strings.add("very"); strings.add("cool");

* String message = String.join("-", strings);

* //message returned is: "Java-is-very-cool"

* }

*

* Note that if an individual element is {@codenull}, then {@code"null"} is added.

*

*@paramdelimiter a sequence of characters that is used to separate each

* of the {@codeelements} in the resulting {@codeString}

*@paramelements an {@codeIterable} that will have its {@codeelements}

* joined together.

*

*@returna new {@codeString} that is composed from the {@codeelements}

* argument

*

*@throwsNullPointerException If {@codedelimiter} or {@codeelements}

* is {@codenull}

*

*@see#join(CharSequence,CharSequence...)

*@seejava.util.StringJoiner

*@since1.8*/

public staticString join(CharSequence delimiter,

Iterable extends CharSequence>elements) {

Objects.requireNonNull(delimiter);

Objects.requireNonNull(elements);

StringJoiner joiner= newStringJoiner(delimiter);for(CharSequence cs: elements) {

joiner.add(cs);

}returnjoiner.toString();

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐