Combine multiple strings into one JAVA

47,056

Solution 1

It is better to use StringBuilder

 StringBuilder sb= new StringBuilder();

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

Solution 2

Or we can use Java 8 Stream

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

Or use the StringJoiner class

Directly Use StringJoiner class

Or the StringBuilder class

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

Solution 3

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

Solution 4

Maybe you are looking only for

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

Reference

Solution 5

Solution 1: (If you don't need a delimiter)


I would recommend using 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());
}

You can use it like this:

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

Solution 2 using StringJoiner (Java 8+):


This is from 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+):


This is a functionality from Java 8 Stream API.

List<String> stringList = Arrays.asList("string1", "string2", "string3");
 
String concatString = stringList.stream().collect(Collectors.joining(", "));
 
assertEquals("string1, string2, string3", concatString);
Share:
47,056
user2762881
Author by

user2762881

Updated on November 10, 2020

Comments

  • user2762881
    user2762881 over 3 years

    I have a set of strings which want to combine into one String with all sentences separated with coma like (*.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<String> setInput = new TreeSet();
    setInput.add("Dog");
    setInput.add("Cat");
    setInput.add("Mouse");
    

    to produce the string:

    ,Dog,,Cat,,Mouse,
    
  • Pshemo
    Pshemo over 10 years
    or even sb.append(",").append(tempString).append(",");
  • Ruchira Gayan Ranaweera
    Ruchira Gayan Ranaweera over 10 years
    @Pshemo yes, it is better. I added your suggestion.
  • Nandkumar Tekale
    Nandkumar Tekale over 10 years
    +1 you can delete last inserted comma using sb.deleteCharAt(sb.lastIndexOf(",")); if it is not necessary
  • simoconfa
    simoconfa almost 6 years
    String finalS = finalStringb.toString() really helps me!
  • O.O.Balance
    O.O.Balance almost 6 years
    Won't match the desired output of ,Dog,,Cat,,Mouse,.
  • Dalton
    Dalton almost 6 years
    Okay so String csvString = "," + String.join(",,", string1, string2, string3) +"," ;
  • Rituraj
    Rituraj about 3 years
    code for removing of last delimiter (comma in this case) doesn't looks kind of old school, I would suggest StringJoiner or StringJoiner through java streams as I mentioned in the answer below. That has nice aesthetics :)
  • Ruchira Gayan Ranaweera
    Ruchira Gayan Ranaweera almost 3 years
    @Rituraj it is up to you to decide. if you are going for Java 8 yes. you can. This question originally posted even before Java 8.
  • Rituraj
    Rituraj almost 3 years
    Yeah. I know it was an old question, I just tried to provide a context, in case someone is using later versions of java. For prior versions I generally preferred Guava from google