一个快速简便的方式加入数组元素与Java中的分隔符(分裂的对面) [英] A quick and easy way to join array elements with a separator (the opposite of split) in Java

查看:250
本文介绍了一个快速简便的方式加入数组元素与Java中的分隔符(分裂的对面)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅Related

我在寻找一个快速简便的方法来做到分裂的完全相反
这样就会造成 [A,B,C] 成为A,B,C

I'm looking for a quick and easy way to do exactly the opposite of split so that it will cause ["a","b","c"] to become "a,b,c"

迭代通过数组要求要么添加条件(如果这不是最后一个元素,添加分离器)或使用子串以除去最后分离

Iterating through an array requires either adding a condition (if this is not the last element, add the seperator) or using substring to remove the last separator.

我敢肯定有一个认证,有效的方式做到这一点(Apache的百科全书?)

I'm sure there is a certified, efficient way to do it (Apache Commons?)

你怎么preFER在项目中做什么呢?

How do you prefer doing it in your projects?

推荐答案

使用Java 8可以在一个非常干净的方式做到这一点:

Using Java 8 you can do this in a very clean way:

String.join(delimiter, elements);

这工作在三个方面:

//directly specifying the elements
String joined1 = String.join(",", "a", "b", "c");

//using arrays
String[] array = new String[] { "a", "b", "c" };
String joined2 = String.join(",", array);

//using iterables
List<String> list = Arrays.asList(array);
String joined3 = String.join(",", list);

这篇关于一个快速简便的方式加入数组元素与Java中的分隔符(分裂的对面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆