StringUtils { public static List<String> split(final String string, final String delimiter) { if (null == string || null == delimiter) { return null; } final ArrayList<String> words = new ArrayList<String>(); int from = 0; int to = 0; int wordLength = delimiter.length(); while (0 <= (to = string.indexOf(delimiter, from))) { words.add(string.substring(from, to)); from = to + wordLength; } if (from < string.length()) { words.add(string.substring(from)); } return words; }  static boolean isEmpty(final CharSequence str); static int length(final CharSequence str); static boolean hasLength(final CharSequence str); static boolean hasText(final CharSequence str); static String nvl(final String... strs); static List<String> split(final String string, final String delimiter); static String split(final String string, final String delimiter, final int index); static String ltrim(final String str); static String rtrim(final String str); static String trim(final String str); static String join(final Iterable<?> values, final String token); static String join(final Object[] values, final String token); static String multiply(final String word, final int repeat); static void multiply(final StringBuilder buffer, final String word, final int repeat); static String repeat(final String word, final int repeat); static String uncapitalize(final String str); static int countMatches(final String str, final char ch); static String removePrefix(final String str, String prefix); static String removeSuffix(final String str, String suffix); static final String NULL_STRING; static final String EMPTY_STRING; }
@Test public void testSplitOnList() { final Object[][] testParameters = new Object[][] {{"2018-06-12", "-", Arrays.asList("2018", "06", "12")}, {"2019.07.14", ".", Arrays.asList("2019", "07", "14")}, {"2020/08/16", "/", Arrays.asList("2020", "08", "16")}}; assertNull(StringUtils.split(null, null)); assertNull(StringUtils.split("2021_09_18", null)); assertNull(StringUtils.split(null, "_")); for (final Object[] testParameter : testParameters) { @SuppressWarnings("unchecked") List<String> expected = (List<String>) testParameter[2]; String string = (String) testParameter[0]; String delimiter = (String) testParameter[1]; for (int i = 0; i < StringUtils.split(string, delimiter).size(); ++i) { assertEquals(expected.get(i), StringUtils.split(string, delimiter).get(i)); } } }
@Test public void testSplitOnString() { final Object[][] testParameters = new Object[][] {{"2018-06-12", "-", 0, "2018"}, {"2019.07.14", ".", 1, "07"}, {"2020/08/16", "/", 2, "16"}}; assertNull(StringUtils.split(null, null, -1)); assertNull(StringUtils.split(null, null, 0)); assertNull(StringUtils.split(null, null, 3)); assertNull(StringUtils.split("2021_09_18", null, -1)); assertNull(StringUtils.split("2021_09_18", null, 0)); assertNull(StringUtils.split("2021_09_18", null, 3)); assertNull(StringUtils.split(null, "_", -1)); assertNull(StringUtils.split(null, "_", 0)); assertNull(StringUtils.split(null, "_", 3)); assertNull(StringUtils.split("2021_09_18", "_", -1)); assertNull(StringUtils.split("2021_09_18", "_", 3)); for (final Object[] testParameter : testParameters) { String expected = (String) testParameter[3]; String string = (String) testParameter[0]; String delimiter = (String) testParameter[1]; int index = (Integer) testParameter[2]; assertEquals(expected, StringUtils.split(string, delimiter, index)); } }