Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

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,
like image 474
user2762881 Avatar asked Sep 11 '13 11:09

user2762881


1 Answers

It is better to use StringBuilder

 StringBuilder sb= new StringBuilder();

for(String tempString:setInput){
   sb.append(",").append(tempString).append(",");   
 }
like image 200
Ruchira Gayan Ranaweera Avatar answered Oct 02 '22 13:10

Ruchira Gayan Ranaweera