简体   繁体   中英

Java - Convert a ArrayList to coma separated strings with double quotes around each string

I am trying to convert a string arrayList to comma separated strings with double quotes around each string. I can write the code, But looking for any existing utility methods out there like apache StringsUtil.

my expected output is like "string1","string2","string3"

String output ="";

for(int i = 0; i < arraylist.size(); i++){ output+="\\""+arraylist.get(i)+"\\""+i==arraylist.size()-1?"":","; }

Or

String str = "\\""+StringUtils.join(arraylist.toArray(new String[0]),"\\",\\"")+"\\"";

use StringBuilder instead to be more efficient, the problem with the previous answer is that it does string concatenation which, in certain cases, with certain types of strings, with certain quantity of concatenations, is going to be inefficient.

Instead, keep .append()'ing the elements you need and do a .toString() at the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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