@Test void split() { String[] elements = StringUtils.split("aa,bb,cc", ","); String[] emptyElements = StringUtils.split("aa", ","); String[] emptyContent = StringUtils.split(",", ","); Assertions.assertEquals(3, elements.length); Assertions.assertEquals(1, emptyElements.length); Assertions.assertEquals(2, emptyContent.length); Assertions.assertAll( () -> Assertions.assertEquals("aa", elements[0]), () -> Assertions.assertEquals("bb", elements[1]), () -> Assertions.assertEquals("cc", elements[2]) ); }
public static String[] split(String text, String delimiter) { int occurrences = countOccurrences(text, delimiter); if (occurrences == 0) { return new String[] { text }; } String[] elements = new String[occurrences + 1]; int arrayIndex = 0; int index = 0; while (index < text.length()) { int currentIndex = text.indexOf(delimiter, index); if (currentIndex == -1) { elements[arrayIndex++] = text.substring(index); break; } elements[arrayIndex++] = text.substring(index, currentIndex); index = currentIndex + delimiter.length(); } if (text.endsWith(delimiter)) { elements[arrayIndex] = EMPTY; } return elements; }
StringUtils { public static String[] split(String text, String delimiter) { int occurrences = countOccurrences(text, delimiter); if (occurrences == 0) { return new String[] { text }; } String[] elements = new String[occurrences + 1]; int arrayIndex = 0; int index = 0; while (index < text.length()) { int currentIndex = text.indexOf(delimiter, index); if (currentIndex == -1) { elements[arrayIndex++] = text.substring(index); break; } elements[arrayIndex++] = text.substring(index, currentIndex); index = currentIndex + delimiter.length(); } if (text.endsWith(delimiter)) { elements[arrayIndex] = EMPTY; } return elements; } }
StringUtils { public static String[] split(String text, String delimiter) { int occurrences = countOccurrences(text, delimiter); if (occurrences == 0) { return new String[] { text }; } String[] elements = new String[occurrences + 1]; int arrayIndex = 0; int index = 0; while (index < text.length()) { int currentIndex = text.indexOf(delimiter, index); if (currentIndex == -1) { elements[arrayIndex++] = text.substring(index); break; } elements[arrayIndex++] = text.substring(index, currentIndex); index = currentIndex + delimiter.length(); } if (text.endsWith(delimiter)) { elements[arrayIndex] = EMPTY; } return elements; } private  StringUtils(); }
StringUtils { public static String[] split(String text, String delimiter) { int occurrences = countOccurrences(text, delimiter); if (occurrences == 0) { return new String[] { text }; } String[] elements = new String[occurrences + 1]; int arrayIndex = 0; int index = 0; while (index < text.length()) { int currentIndex = text.indexOf(delimiter, index); if (currentIndex == -1) { elements[arrayIndex++] = text.substring(index); break; } elements[arrayIndex++] = text.substring(index, currentIndex); index = currentIndex + delimiter.length(); } if (text.endsWith(delimiter)) { elements[arrayIndex] = EMPTY; } return elements; } private  StringUtils(); static String[] splitFirst(String text, String delimiter); static String[] split(String text, String delimiter); static int lastIndexOfBefore(String text, String element, int occurrences); static int lastIndexOf(String text, String element, int toIndex); static String replace(String text, String searchString, String replacement); static String replaceFirst(String text, String pattern, String replacement); static String replaceFirst(String text, String pattern, String replacement, int start); static String replaceRespectively(String text, String pattern, String... values); static String replaceRespectivelyAndSoftly(String text, String pattern, String... values); static String capitalize(String text); static boolean startsWith(String text, Predicate<char[]> filter); static String trimStart(String text); static String trimEnd(String text); static String extractParagraph(String str); static String buildSpace(int spaces); static String repeated(int repetitions, String... elements); static int countOccurrences(String text, String element); static boolean containsCharacter(String text, char... characters); static boolean containsSpecialCharacters(String text); static boolean containsOtherCharacters(String text, char[]... characters); static String join(String... strings); static String toString(@Nullable Object object); static boolean isNumber(String str); static String isEmpty(String text, String exceptionMessage); static boolean isEmpty(String text); }
StringUtils { public static String[] split(String text, String delimiter) { int occurrences = countOccurrences(text, delimiter); if (occurrences == 0) { return new String[] { text }; } String[] elements = new String[occurrences + 1]; int arrayIndex = 0; int index = 0; while (index < text.length()) { int currentIndex = text.indexOf(delimiter, index); if (currentIndex == -1) { elements[arrayIndex++] = text.substring(index); break; } elements[arrayIndex++] = text.substring(index, currentIndex); index = currentIndex + delimiter.length(); } if (text.endsWith(delimiter)) { elements[arrayIndex] = EMPTY; } return elements; } private  StringUtils(); static String[] splitFirst(String text, String delimiter); static String[] split(String text, String delimiter); static int lastIndexOfBefore(String text, String element, int occurrences); static int lastIndexOf(String text, String element, int toIndex); static String replace(String text, String searchString, String replacement); static String replaceFirst(String text, String pattern, String replacement); static String replaceFirst(String text, String pattern, String replacement, int start); static String replaceRespectively(String text, String pattern, String... values); static String replaceRespectivelyAndSoftly(String text, String pattern, String... values); static String capitalize(String text); static boolean startsWith(String text, Predicate<char[]> filter); static String trimStart(String text); static String trimEnd(String text); static String extractParagraph(String str); static String buildSpace(int spaces); static String repeated(int repetitions, String... elements); static int countOccurrences(String text, String element); static boolean containsCharacter(String text, char... characters); static boolean containsSpecialCharacters(String text); static boolean containsOtherCharacters(String text, char[]... characters); static String join(String... strings); static String toString(@Nullable Object object); static boolean isNumber(String str); static String isEmpty(String text, String exceptionMessage); static boolean isEmpty(String text); static final String EMPTY; static final String[] EMPTY_ARRAY; }