Community
Participate
Working Groups
I'm missing a vital method in the otherwise very handy StringUtils class. The attached patch adds a join method that joins the string representation of a collection of objects to one continues string which (for me at least) is often needed to represent collections in the UI.
Created attachment 181377 [details] Patch for StringUtils
I changed the implementation to /** * Joins the string representations of an {@link Iterable} collection of * objects to one continues string while separating the entities with the * provided separator. * <p> * The string representation will be retrieved by String.valueOf() * * @param iterable * The {@link Iterable} number of objects * @param separator * @return The joined String separated by the given separator */ public static String join(final Iterable<? extends Object> iterable, final CharSequence separator) { Iterator<? extends Object> oIt; if (iterable == null || (!(oIt = iterable.iterator()).hasNext())) { return ""; //$NON-NLS-1$ } final StringBuilder sb = new StringBuilder(String.valueOf(oIt.next())); while (oIt.hasNext()) { sb.append(separator).append(oIt.next()); } return sb.toString(); } This way we habe null-handling and don´t create the StringBuilder if we don´t need to. Added StringUtilsTest#testJoin()
commit