博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 8 string_String.join() --Java8中String类新增方法
阅读量:4475 次
发布时间:2019-06-08

本文共 2466 字,大约阅读时间需要 8 分钟。

/*** 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();

}

转载地址:http://pcips.baihongyu.com/

你可能感兴趣的文章
no crontab for root 解决方案
查看>>
IAP 协议
查看>>
设计模式
查看>>
CentOS7修改网卡为eth0
查看>>
Best Quality CAT Caterpillar ET Diagnostic Adapter III
查看>>
定向转发和重定向实现 <select >下拉表单数据传送
查看>>
数组与字符串一(概念和基础)
查看>>
用cocos2d-html5做的消除类游戏《英雄爱消除》(4)——游戏结束
查看>>
CNUOJ 535 黑魔法师之门
查看>>
18. Maven 的单模块 / 多模块之 Spring MVC + Spring + Mybatis 项目讲解(重点)
查看>>
vs2017 F5不会自动编译了
查看>>
hdu 1028
查看>>
取消 Win7 驱动数字签名认证 WIN7 X64 系统
查看>>
East Brother Video Indoor Monitor
查看>>
Python基础数据类型2
查看>>
linux Shell脚本编码格式
查看>>
【转】人脸表情识别综述
查看>>
【转】OpenCV图像处理 图像的点运算 ( 灰度直方图 )
查看>>
斜率优化DP学习笔记
查看>>
vim 操作命令大全(转)
查看>>