Strings { public static List<String> split(String string, char c) { List<String> list = new ArrayList<String>(); int lastFoundIndex = -1; int nextFoundIndex; while ((nextFoundIndex = string.indexOf(c, lastFoundIndex + 1)) != -1) { String value = string.substring(lastFoundIndex + 1, nextFoundIndex).trim(); if (!value.isEmpty()) { list.add(value); } lastFoundIndex = nextFoundIndex; } String value = string.substring(lastFoundIndex + 1).trim(); if (!value.isEmpty()) { list.add(value); } return list; } private  Strings(); static String nullToEmpty(@Nullable String string); static List<String> split(String string, char c);  }
@Test public void shouldSplit() { assertThat(Strings.split("c,s,v", ',')).contains("c", "s", "v"); assertThat(Strings.split("c.s.v", '.')).contains("c", "s", "v"); assertThat(Strings.split("c,s,v,", ',')).contains("c", "s", "v"); assertThat(Strings.split(" , csv , x, y ", ',')).contains("csv", "x", "y"); assertThat(Strings.split(",,,", ',')).isEmpty(); }