将多个字符串组合成一个JAVA

时间:2022-06-20 21:14:55

I have a set of strings which want to combine into one String with all sentences separated with coma like (*.csv)

我有一组字符串想要组合成一个字符串,所有句子用逗号分隔,如(* .csv)

here is how it goes with me:

这是我的方式:

String dataContainer;

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

This doesn't work with me :(
But it should do for Set ex:

这不适合我:(但它应该为Set ex做:

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

to produce the string:

生成字符串:

,Dog,,Cat,,Mouse,

6 个解决方案

#1


16  

It is better to use StringBuilder

最好使用StringBuilder

 StringBuilder sb= new StringBuilder();

for(String tempString:setInput){
   sb.append(",").append(tempString).append(",");   
 }

#2


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.

但上述方法会导致多个String创建。

But I suggest to go for StringBuilder.

但我建议去StringBuilder。

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

String finalS = finalStringb.toString();

#3


3  

Or we can use Java 8 Stream

或者我们可以使用Java 8 Stream

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

Or

Directly Use StringJoiner class

Or

new StringBuilder().add("A").add("B").toString()

etc

#4


2  

Alternatively, if you are using Java 8, you could try something like this:

或者,如果您使用的是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));
}

The output is:

输出是:

,Cat,,Dog,,Mouse,

Although, I'm a little unsure to why you would want 2 commas in between each element and a comma at the start and end. If you just want one comma separating each element (and no comma at the start or end), modify the join(Set<String>) to look like this:

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

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

After doing so, the new output would be:

完成后,新输出将是:

Cat,Dog,Mouse

#5


1  

org.apache.commons.lang.StringUtils.join() can come in handy

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

#6


1  

Maybe you are looking only for

也许你只是在寻找

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

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

Reference

#1


16  

It is better to use StringBuilder

最好使用StringBuilder

 StringBuilder sb= new StringBuilder();

for(String tempString:setInput){
   sb.append(",").append(tempString).append(",");   
 }

#2


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.

但上述方法会导致多个String创建。

But I suggest to go for StringBuilder.

但我建议去StringBuilder。

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

String finalS = finalStringb.toString();

#3


3  

Or we can use Java 8 Stream

或者我们可以使用Java 8 Stream

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

Or

Directly Use StringJoiner class

Or

new StringBuilder().add("A").add("B").toString()

etc

#4


2  

Alternatively, if you are using Java 8, you could try something like this:

或者,如果您使用的是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));
}

The output is:

输出是:

,Cat,,Dog,,Mouse,

Although, I'm a little unsure to why you would want 2 commas in between each element and a comma at the start and end. If you just want one comma separating each element (and no comma at the start or end), modify the join(Set<String>) to look like this:

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

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

After doing so, the new output would be:

完成后,新输出将是:

Cat,Dog,Mouse

#5


1  

org.apache.commons.lang.StringUtils.join() can come in handy

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

#6


1  

Maybe you are looking only for

也许你只是在寻找

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

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

Reference