将多个字符串合并为一个 JAVA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18740501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:37:56  来源:igfitidea点击:

Combine multiple strings into one JAVA

javastringconcatenation

提问by user2762881

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

我有一组strings想要合并成一个String所有句子都用昏迷分隔的句子(*.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,

回答by Suresh Atta

What You are doing is intializingyour result string each time.

您所做的是 intializing每次的结果字符串。

Actually ,you want to do

其实你想做

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

But the above approach causes multiple Stringcreations.

但是上述方法会导致多次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();

回答by Ruchira Gayan Ranaweera

It is better to use StringBuilder

最好使用 StringBuilder

 StringBuilder sb= new StringBuilder();

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

回答by Josh M

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,

,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 个逗号,并在开头和结尾使用逗号。如果您只想用一个逗号分隔每个元素(并且在开头或结尾处没有逗号),请将 修改join(Set<String>)为如下所示:

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

Cat,Dog,Mouse

回答by Bukhtawar

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

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

回答by Rituraj

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

等等

回答by Dalton

Maybe you are looking only for

也许你只是在寻找

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

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

Reference

参考