StringUtils { public static String strJoin(List<String> entries, String separator) { StringBuilder sb = new StringBuilder(); boolean addSeparatorFlag = false; if (entries != null) { for (String entry : entries) { if (!TextUtils.isEmpty(entry)) { if (addSeparatorFlag && separator != null) { sb.append(separator); } else { addSeparatorFlag = true; } sb.append(entry); } } } return sb.toString(); } private  StringUtils(); static String appendOptional(String first, String second); static String appendOptional(String first, String second, String separator); static String strJoin(List<String> entries, String separator); static int addLineSeparator(SpannableStringBuilder sb, boolean force); static int addLineSeparator(SpannableStringBuilder sb); static Spanned createSpannedHtmlLink(String name, String url); static final String COMMA_SEPARATOR; static final String LINE_SEPARATOR; static final String HTML_LINE_SEPARATOR; static final String ZERO_WIDTH_SPACE; }
@Test public void should_append_list_of_string_with_separator() { List<String> entries = Arrays.asList("1", "2", "3"); String separator = ":"; String result = StringUtils.strJoin(entries, separator); assertThat(result).isEqualTo("1:2:3"); }
@Test public void should_append_list_of_string_without_separator_if_null() { List<String> entries = Arrays.asList("1", "2", "3"); String separator = null; String result = StringUtils.strJoin(entries, separator); assertThat(result).isEqualTo("123"); }
@Test public void should_return_empty_string_if_nothing_to_join() { List<String> entries = null; String separator = ":"; String result = StringUtils.strJoin(entries, separator); assertThat(result).isEmpty(); }