ski*_*iwi 694
使用Java 8,您可以以非常干净的方式执行此操作:
String.join(delimiter, elements);
Run Code Online (Sandbox Code Playgroud)
这有三种方式:
1)直接指定元素
String joined1 = String.join(",", "a", "b", "c");
Run Code Online (Sandbox Code Playgroud)
2)使用数组
String[] array = new String[] { "a", "b", "c" };
String joined2 = String.join(",", array);
Run Code Online (Sandbox Code Playgroud)
3)使用iterables
List<String> list = Arrays.asList(array);
String joined3 = String.join(",", list);
Run Code Online (Sandbox Code Playgroud)
nd.*_*nd. 114
对于这个特殊问题,我更喜欢Google Collections而不是Apache StringUtils:
Joiner.on(separator).join(array)
Run Code Online (Sandbox Code Playgroud)
与StringUtils相比,Joiner API具有流畅的设计并且更加灵活,例如null
元素可以被跳过或替换为占位符.此外,Joiner
还具有使用键和值之间的分隔符连接地图的功能.
coo*_*ird 105
Apache Commons Lang确实有一种StringUtils.join
方法可以将String
数组与指定的分隔符连接在一起.
例如:
String[] s = new String[] {"a", "b", "c"};
String joined = StringUtils.join(s, ","); // "a,b,c"
Run Code Online (Sandbox Code Playgroud)
但是,我怀疑,正如你所提到的,在上述方法的实际实现中必须有某种条件或子串处理.
如果我要执行String
加入并且没有任何其他理由使用Commons Lang,我可能会自己动手来减少对外部库的依赖数量.
小智 45
没有任何第三方的快速简单的解决方案包括.
public static String strJoin(String[] aArr, String sSep) {
StringBuilder sbStr = new StringBuilder();
for (int i = 0, il = aArr.length; i < il; i++) {
if (i > 0)
sbStr.append(sSep);
sbStr.append(aArr[i]);
}
return sbStr.toString();
}
Run Code Online (Sandbox Code Playgroud)
Rol*_*man 24
"我确信有一种经过认证的有效方法(Apache Commons?)"
是的,明显的是它
StringUtils.join(array, separator)
Run Code Online (Sandbox Code Playgroud)
eiv*_*ndw 19
使用Java 1.8,有一个新的StringJoiner类 - 所以不需要Guava或Apache Commons:
String str = new StringJoiner(",").add("a").add("b").add("c").toString();
Run Code Online (Sandbox Code Playgroud)
或直接使用新流api的集合:
String str = Arrays.asList("a", "b", "c").stream().collect(Collectors.joining(","));
Run Code Online (Sandbox Code Playgroud)
ale*_*exm 18
你甚至可以更容易地使用Arrays,因此你将获得一个String,其中数组的值由","分隔
String concat = Arrays.toString(myArray);
Run Code Online (Sandbox Code Playgroud)
所以你最终得到这个:concat ="[a,b,c]"
更新
然后,您可以使用Jeff建议的子字符串去掉括号
concat = concat.substring(1, concat.length() -1);
Run Code Online (Sandbox Code Playgroud)
所以你最终得到了concat ="a,b,c"
wad*_*man 16
您可以将replace和replaceAll与正则表达式一起使用.
String[] strings = {"a", "b", "c"};
String result = Arrays.asList(strings).toString().replaceAll("(^\\[|\\]$)", "").replace(", ", ",");
Run Code Online (Sandbox Code Playgroud)
因为Arrays.asList().toString()
产生:"[a,b,c]",我们做一个replaceAll
删除第一个和最后一个括号然后(可选)你可以改变","(你的新分隔符)的","序列.
剥离版本(更少的字符):
String[] strings = {"a", "b", "c"};
String result = ("" + Arrays.asList(strings)).replaceAll("(^.|.$)", "").replace(", ", "," );
Run Code Online (Sandbox Code Playgroud)
正则表达式非常强大,特别是String方法"replaceFirst"和"replaceAll".试一试.
所有这些其他答案都包括运行时开销......比如使用ArrayList.toString().replaceAll(...)非常浪费.
我会给你一个零开销的最优算法; 它看起来不像其他选项那么漂亮,但在内部,这是他们都在做的事情(在成堆的其他隐藏检查,多个数组分配和其他crud之后).
由于您已经知道正在处理字符串,因此可以通过手动执行所有操作来节省大量数组分配.这不是很好,但是如果你跟踪其他实现所做的实际方法调用,你会发现它的运行时开销最小.
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);
}
Run Code Online (Sandbox Code Playgroud)
这个选项快速而清晰:
public static String join(String separator, String... values) {
StringBuilder sb = new StringBuilder(128);
int end = 0;
for (String s : values) {
if (s != null) {
sb.append(s);
end = sb.length();
sb.append(separator);
}
}
return sb.substring(0, end);
}
Run Code Online (Sandbox Code Playgroud)
这个小功能总是派上用场。
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();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
467922 次 |
最近记录: |