JavaScript有Array.join()
js>["Bill","Bob","Steve"].join(" and ")
Bill and Bob and Steve
Java有这样的东西吗?我知道我可以用StringBuilder自己拼凑一些东西:
static public String join(List<String> list, String conjunction)
{
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String item : list)
{
if (first)
first = false;
else
sb.append(conjunction);
sb.append(item);
}
return sb.toString();
}
. .但是如果像这样的东西已经是JDK的一部分,那么这样做就没有意义了。
你可以从Spring框架的StringUtils中使用它。我知道它已经被提到过,但是实际上您可以只使用这段代码,它就可以立即工作,而不需要Spring。
// from https://github.com/spring-projects/spring-framework/blob/master/spring-core/src/main/java/org/springframework/util/StringUtils.java
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class StringUtils {
public static String collectionToDelimitedString(Collection<?> coll, String delim, String prefix, String suffix) {
if(coll == null || coll.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
Iterator<?> it = coll.iterator();
while (it.hasNext()) {
sb.append(prefix).append(it.next()).append(suffix);
if (it.hasNext()) {
sb.append(delim);
}
}
return sb.toString();
}
}
EDIT
我还注意到toString()底层实现问题,以及包含分隔符的元素,但我认为我是偏执狂。
鉴于我对此有两点看法,我将我的答案改为:
static String join( List<String> list , String replacement ) {
StringBuilder b = new StringBuilder();
for( String item: list ) {
b.append( replacement ).append( item );
}
return b.toString().substring( replacement.length() );
}
这看起来和最初的问题很相似。
所以如果你不想把整个罐子添加到你的项目中,你可以使用这个。
我认为你原来的代码没有问题。实际上,每个人都建议的替代方案看起来几乎相同(尽管它做了一些额外的验证)
这里有Apache 2.0许可证。
public static String join(Iterator iterator, String separator) {
// handle null, zero and one elements before building a buffer
if (iterator == null) {
return null;
}
if (!iterator.hasNext()) {
return EMPTY;
}
Object first = iterator.next();
if (!iterator.hasNext()) {
return ObjectUtils.toString(first);
}
// two or more elements
StringBuffer buf = new StringBuffer(256); // Java default is 16, probably too small
if (first != null) {
buf.append(first);
}
while (iterator.hasNext()) {
if (separator != null) {
buf.append(separator);
}
Object obj = iterator.next();
if (obj != null) {
buf.append(obj);
}
}
return buf.toString();
}
现在我们知道了,谢谢开源
使用java 8收集器,这可以用以下代码完成:
Arrays.asList("Bill", "Bob", "Steve").stream()
.collect(Collectors.joining(" and "));
此外,java 8中最简单的解决方案:
String.join(" and ", "Bill", "Bob", "Steve");
or
String.join(" and ", Arrays.asList("Bill", "Bob", "Steve"));
使用Java .util. stringjoiner的Java 8解决方案
Java 8有一个StringJoiner类。但您仍然需要编写一些样板文件,因为它是Java。
StringJoiner sj = new StringJoiner(" and ", "" , "");
String[] names = {"Bill", "Bob", "Steve"};
for (String name : names) {
sj.add(name);
}
System.out.println(sj);