Last active
August 29, 2015 14:21
-
-
Save Fi5t/9c3fa3337b3efa9e3765 to your computer and use it in GitHub Desktop.
Join collection of strings into comma-separated string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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