KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Given a Collection of Strings, how would you join them in plain Java, without using an external Library? Given these variables: Collection<String> data = Arrays.asList("Snap", "Crackle", "Pop"); String separator = ", "; String joined; // let's create this, shall we? This is how I'd do it in Guava : joined = Joiner.on(separator).join(data); And in Apache Commons / Lang : joined = StringUtils.join(data, separator); But in plain Java, is there really no better way than this? StringBuilder sb = new StringBuilder(); for(String item : data){ if(sb.length()>0)sb.append(separator); sb.append(item); } joined = sb.toString();
Tags (comma-separated)
Save Edits
Cancel