参见相关。net问题
我正在寻找一种快速简单的方法来做完全相反的分裂 这样["a","b","c"]就会变成"a,b,c"
遍历数组需要添加一个条件(如果这不是最后一个元素,则添加分隔符)或使用substring删除最后一个分隔符。
我相信有一种经过认证的、有效的方法来做到这一点(Apache Commons?)
你喜欢在你的项目中怎么做?
参见相关。net问题
我正在寻找一种快速简单的方法来做完全相反的分裂 这样["a","b","c"]就会变成"a,b,c"
遍历数组需要添加一个条件(如果这不是最后一个元素,则添加分隔符)或使用substring删除最后一个分隔符。
我相信有一种经过认证的、有效的方法来做到这一点(Apache Commons?)
你喜欢在你的项目中怎么做?
当前回答
使用Java 8,你可以用一种非常干净的方式做到这一点:
String.join(delimiter, elements);
这有三种方式:
1)直接指定要素
String joined1 = String.join(",", "a", "b", "c");
2)使用数组
String[] array = new String[] { "a", "b", "c" };
String joined2 = String.join(",", array);
3)使用可迭代对象
List<String> list = Arrays.asList(array);
String joined3 = String.join(",", list);
其他回答
Apache Commons Lang确实有一个StringUtils。join方法,将字符串数组与指定的分隔符连接在一起。
例如:
String[] s = new String[] {"a", "b", "c"};
String joined = StringUtils.join(s, ","); // "a,b,c"
然而,正如您所提到的,我怀疑在上述方法的实际实现中一定存在某种条件或子字符串处理。
如果我要执行String连接,并且没有任何其他使用Commons Lang的原因,我可能会使用自己的来减少对外部库的依赖数量。
所有这些其他答案都包括运行时开销……比如使用ArrayList.toString(). replaceall(…),这是非常浪费的。
我会给出零开销的最优算法; 它看起来不像其他选项那么漂亮,但在内部,这是它们都在做的事情(在成堆的其他隐藏检查、多个数组分配和其他杂事之后)。
因为您已经知道您正在处理字符串,所以您可以通过手动执行所有操作来节省大量的数组分配。这并不漂亮,但如果跟踪其他实现所执行的实际方法调用,您将看到它的运行时开销可能最少。
public static String join(String separator, String ... values) {
if (values.length==0)return "";//need at least one element
//all string operations use a new array, so minimize all calls possible
char[] sep = separator.toCharArray();
// determine final size and normalize nulls
int totalSize = (values.length - 1) * sep.length;// separator size
for (int i = 0; i < values.length; i++) {
if (values[i] == null)
values[i] = "";
else
totalSize += values[i].length();
}
//exact size; no bounds checks or resizes
char[] joined = new char[totalSize];
int pos = 0;
//note, we are iterating all the elements except the last one
for (int i = 0, end = values.length-1; i < end; i++) {
System.arraycopy(values[i].toCharArray(), 0,
joined, pos, values[i].length());
pos += values[i].length();
System.arraycopy(sep, 0, joined, pos, sep.length);
pos += sep.length;
}
//now, add the last element;
//this is why we checked values.length == 0 off the hop
System.arraycopy(values[values.length-1].toCharArray(), 0,
joined, pos, values[values.length-1].length());
return new String(joined);
}
“我相信有一种经过认证的、有效的方法来做到这一点(Apache Commons?)”
是的,很明显
StringUtils.join(array, separator)
http://www.java2s.com/Code/JavaAPI/org.apache.commons.lang/StringUtilsjoinObjectarrayStringseparator.htm
这个小函数总是会派上用场。
public static String join(String[] strings, int startIndex, String separator) {
StringBuffer sb = new StringBuffer();
for (int i=startIndex; i < strings.length; i++) {
if (i != startIndex) sb.append(separator);
sb.append(strings[i]);
}
return sb.toString();
}
使用Java 8,你可以用一种非常干净的方式做到这一点:
String.join(delimiter, elements);
这有三种方式:
1)直接指定要素
String joined1 = String.join(",", "a", "b", "c");
2)使用数组
String[] array = new String[] { "a", "b", "c" };
String joined2 = String.join(",", array);
3)使用可迭代对象
List<String> list = Arrays.asList(array);
String joined3 = String.join(",", list);