Skip to content

Instantly share code, notes, and snippets.

@Fi5t
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save Fi5t/9c3fa3337b3efa9e3765 to your computer and use it in GitHub Desktop.

Select an option

Save Fi5t/9c3fa3337b3efa9e3765 to your computer and use it in GitHub Desktop.
Join collection of strings into comma-separated string
public static String join(String separator, Iterator<String> iterator) {
StringBuilder builder = new StringBuilder();
while (iterator.hasNext()) {
String token = iterator.next();
if (token != null && !token.isEmpty()) {
builder.append(token);
break;
}
}
while (iterator.hasNext()) {
String token = iterator.next();
if (token != null && !token.isEmpty()) {
builder.append(separator);
builder.append(token);
}
}
return builder.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment