7

我有一组strings想要组合成一个String,所有句子都用逗号分隔(*.csv)

这是我的情况:

String dataContainer;

for(String tempString:setInput){
     String finalString = "," + tempString + "," ;   
}

这对我不起作用:(
但它应该适用于 Set ex:

Set<String> setInput = new TreeSet();
setInput.add("Dog");
setInput.add("Cat");
setInput.add("Mouse");

产生字符串:

,Dog,,Cat,,Mouse,
4

7 回答 7

17

最好使用StringBuilder

 StringBuilder sb= new StringBuilder();

for(String tempString:setInput){
   sb.append(",").append(tempString).append(",");   
 }
于 2013-09-11T11:59:49.083 回答
7

或者我们可以使用 Java 8 Stream

String joined = Stream.of("A", "B", "C").collect(Collectors.joining("delimiter", "prefix", "suffix"));

或者使用 StringJoiner 类

Directly Use StringJoiner class

或 StringBuilder 类

new StringBuilder().add("A").add("B").toString()
于 2017-07-26T22:34:33.253 回答
6

What You are doing is intializing your result string each time.

Actually ,you want to do

String finalString ="";
for(String tempString:setInput){
      finalString += "," + tempString + "," ;   
}

But the above approach causes multiple String creations.

But I suggest to go for StringBuilder.

 StringBuilder finalStringb =new StringBuilder();
    for(String tempString:setInput){
          finalStringb.append(",").append(tempString).append(",") ;   
    }

String finalS = finalStringb.toString();
于 2013-09-11T11:57:56.737 回答
4

也许你只是在寻找

String csvString = "," + String.join(",,", string1, string2, string3) +"," ;

参考

于 2018-07-18T19:03:06.023 回答
3

解决方案1:(如果您不需要分隔符)


我建议使用concat(Object... objects)from org.assertj.core.util.String

public static String concat(Object... objects) {
    return Arrays.isNullOrEmpty(objects) ? null : (String)java.util.Arrays.stream(objects).map(String::valueOf).collect(Collectors.joining());
}

你可以像这样使用它:

  concat("string1", "string2", "string3", "string4");

使用 StringJoiner (Java 8+) 的解决方案 2:


这是从java.util。您甚至可以选择指定前缀和后缀。

StringJoiner stringJoiner = new StringJoiner(", ");
 
stringJoiner.add("string1");
stringJoiner.add("string2");
stringJoiner.add("string3");
 
assertEquals("string1, string2, string3", stringJoiner.toString());

使用 Collectors.joining (Java 8+) 的解决方案 3:


这是来自 Java 8 Stream API 的功能。

List<String> stringList = Arrays.asList("string1", "string2", "string3");
 
String concatString = stringList.stream().collect(Collectors.joining(", "));
 
assertEquals("string1, string2, string3", concatString);
于 2020-09-01T12:00:42.503 回答
2

或者,如果您使用的是 Java 8,则可以尝试以下操作:

public static String join(final Set<String> set){
    return new StringBuilder(",").append(set.stream().collect(Collectors.joining(",,"))).append(",").toString();
}

public static void main(String args[]){
    Set<String> setInput = new TreeSet<>();
    setInput.add("Dog");
    setInput.add("Cat");
    setInput.add("Mouse");
    System.out.println(join(setInput));
}

输出是:

,Cat,,Dog,,Mouse,

虽然,我有点不确定为什么你会在每个元素之间使用 2 个逗号,并在开头和结尾使用逗号。如果您只想用一个逗号分隔每个元素(并且开头或结尾没有逗号),请将 修改join(Set<String>)为如下所示:

public static String join(final Set<String> set){
    return set.stream().collect(Collectors.joining(",")); //change "," to ", " for spacing
}

这样做之后,新的输出将是:

Cat,Dog,Mouse

于 2013-09-11T12:06:01.397 回答
1

org.apache.commons.lang.StringUtils.join() 可以派上用场

于 2017-03-22T16:33:52.787 回答