简体   繁体   English

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

[英]Combine multiple strings into one JAVA

I have a set of strings which want to combine into one String with 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,

It is better to use StringBuilder最好使用StringBuilder

 StringBuilder sb= new StringBuilder();

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

Or we can use Java 8 Stream或者我们可以使用 Java 8 Stream

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

Or use the StringJoiner class或者使用 StringJoiner 类

Directly Use StringJoiner class

Or the StringBuilder class或者 StringBuilder 类

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

What You are doing is intializing your result string each time.您正在做的是每次intializing您的结果字符串。

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();

Maybe you are looking only for也许你只是在寻找

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

Reference 参考

Solution 1: (If you don't need a delimiter)解决方案1:(如果您不需要分隔符)


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

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

You can use it like this:你可以这样使用它:

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

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


This is from java.util .这是来自java.util You even have the option to specify a prefix and suffix.您甚至可以选择指定前缀和后缀。

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

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


This is a functionality from Java 8 Stream API.这是 Java 8 Stream API 的一项功能。

List<String> stringList = Arrays.asList("string1", "string2", "string3");
 
String concatString = stringList.stream().collect(Collectors.joining(", "));
 
assertEquals("string1, string2, string3", concatString);

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.虽然,我有点不确定为什么要在每个元素之间使用 2 个逗号,并在开头和结尾使用逗号。 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:如果您只想用逗号分隔每个元素(并且在开头或结尾没有逗号),请将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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM