当前位置:网站首页>String. Join() and stringutils Join () gracefully solves the splicing of arrays or collections
String. Join() and stringutils Join () gracefully solves the splicing of arrays or collections
2022-04-22 20:31:00 【New ape and horse】
Catalog
StringUtils.join() Realization
Comparison of the two implementation methods
You may have encountered an array or collection in a certain way “,”、“-”、“.” The need to splice characters into new strings , The spliced string will look like this a,b,c perhaps a-b-c perhaps a.b.c etc. .
You might want to go through the splicing , Remove the last splice character .
public static void main(String[] args) {
List<String> list = new ArrayList<>(Arrays.asList(new String[]{"a", "b", "c"}));
StringBuilder stringBuilder = new StringBuilder();
for (String s : list) {
stringBuilder.append(s + ",");
}
System.out.println(stringBuilder.substring(0, stringBuilder.length() - 1));
}
Or think about traversing first , The last element of the array is handled separately .
public static void main(String[] args) {
List<String> list = new ArrayList<>(Arrays.asList(new String[]{"a", "b", "c"}));
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < list.size() -1 ;i ++) {
stringBuilder.append(list.get(i) + ",");
}
System.out.println(stringBuilder.append(list.get(list.size() - 1)).toString());
}
The above two methods either deal with the last spliced special character 、 Or you need to process the last character separately , Not particularly elegant , Do we have an elegant way to implement it . Don't say , There are really two ways to implement .
StringUtils.join() Realization
pom rely on
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
api explain

Code implementation
public static void main(String[] args) {
List<String> list = new ArrayList<>(Arrays.asList(new String[]{"a", "b", "c"}));
System.out.println(StringUtils.join(list, ","));
}
String.join() Realization
pom rely on
String.join() yes JDK8 The new method , No need to introduce external dependencies .
api explain

Code implementation
public static void main(String[] args) {
List<String> list = new ArrayList<>(Arrays.asList(new String[]{"a", "b", "c"}));
System.out.println(String.join(",", list));
}
StringUtils.join and String.join() Both are one line of code and you're done , Is it elegant ?
Comparison of the two implementation methods
Parameter sequence :StringUtils.join() The first parameter is an array or collection , The second parameter is the concatenation character ;String.join() The first parameter is the concatenation character , The second parameter array or collection .
Parameter range :StringUtils.join() You can pass in Integer Or other types of collections or arrays ; String.join() You can pass in and implement charSequence A collection or array of interface types .
conclusion : If it is a collection or array of string type, it is recommended to use String.join(), For other types, it is recommended to use StringUtils.join().
版权声明
本文为[New ape and horse]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221912434503.html
边栏推荐
- 虚拟机搭建安装Pulsar环境教程(开发测试使用)
- Swift 协议的使用
- Mysql, in the unique index of combination, handles the problem of null value
- JWT token practice and problem solving
- 普通函数做友元(用举例了解友函数)
- Can you really use MySQL explain?
- 用MarkDown写PPT
- DA14580作为server发送数据
- Write a gateway service, understand more thoroughly!
- 网站:fakeimg.pl(文字 --> 图片)
猜你喜欢
After five years of Android, I successfully joined Tencent with this 190 page interview information

Matlab learning notes - calculate eigenvectors and manually execute PCA

Podcasts about the universe

How to make robots more like "people" and make slams more flexible?

MySQL的explain,你真的会用吗?
![[ DASCTF Oct X ]迷路的魔法少女](/img/1a/b42e8b1189613c131181ce41953508.png)
[ DASCTF Oct X ]迷路的魔法少女

2020团队设计天梯赛(部分)
![[suggestions collection] no highlights in the interview](/img/65/40d65aad750ce99ee7ef134e4f3ee3.png)
[suggestions collection] no highlights in the interview

What are the types of documenter examination questions? How to prepare for the documenter examination of the construction department

一文讲透,商业智能BI的未来形态,发展现状及前景分析|推荐收藏
随机推荐
MATLAB学习笔记 - 计算特征向量手动执行PCA
Some operations of MySQL 5.6 under Linux
域名解析过程
Review of SSM framework
How to execute stored procedures in Navicat?
Filebeat
Error running ‘JeecgSystemApplication‘: Command line is too long. Shorten command line for JeecgSyst
Cannot proceed because system tables used by Event Scheduler were found damaged
ZTNA (Zero Trust Network Access)
华为机试题——HJ72 百钱买百鸡问题
【dfs】386. 字典序排数
DA14580作为server发送数据
Team work principles
MySQL development skills
Boot implementation of IAP
About the reload of log4j2 and the output of different levels of logs to different log files
普通函数做友元(用举例了解友函数)
产品和服务谁重要,长安福特告诉你“全都要”
IOS开发之——数据库-常见操作(02)
Some considerations for pointers and objects