android string.join java8_String.join() --Java8中String类新增方法
/*** 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 = ...
/*** 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();
}
更多推荐
所有评论(0)