StringUtils { public static String[] split(String str) { return split(str, null, -1); }  static String[] split(String str); static String[] split(String str, String separatorChars); static String[] split(String str, char separatorChar); static String[] split(String str, String separatorChars, int max); static String[] splitByWholeSeparator(String str, String separator); static String[] splitByWholeSeparator(String str, String separator, int max); static boolean hasText(String str); static boolean hasText(CharSequence str); static boolean hasLength(CharSequence str); static boolean hasLength(String str); static String replace(String s, Map<String, Object> map); static String replace(String s, Object... objs); static String replaceStr(String s, String sub, String with); static String escapeXML(String str); static String unicodeToString(String s); static String getFilenameExtension(String path); static String getFilename(String path); static byte[] getUtf8Bytes(String string); static byte[] getBytes(String string); static int toInt(String string, int from); static void append(StringBuilder buf, byte b, int base); static void append2digits(StringBuilder buf, int i); static String asciiToLowerCase(String s); static String normalizeCharset(String s); static String normalizeCharset(String s, int offset, int length); static boolean isHex(String str, int offset, int length); static String truncate(String str, int maxSize); static final String EMPTY; static final String[] EMPTY_STRING_ARRAY; }
@Test void testSplit() { String byteRangeSet = "500-"; String[] byteRangeSets = StringUtils.split(byteRangeSet, ','); System.out.println(Arrays.toString(byteRangeSets)); assertEquals(byteRangeSets.length, 1); byteRangeSet = "500-,"; byteRangeSets = StringUtils.split(byteRangeSet, ','); System.out.println(Arrays.toString(byteRangeSets)); assertEquals(byteRangeSets.length, 1); byteRangeSet = ",500-,"; byteRangeSets = StringUtils.split(byteRangeSet, ','); System.out.println(Arrays.toString(byteRangeSets)); assertEquals(byteRangeSets.length, 1); byteRangeSet = ",500-,"; byteRangeSets = StringUtils.split(byteRangeSet, ","); System.out.println(Arrays.toString(byteRangeSets)); assertEquals(byteRangeSets.length, 1); byteRangeSet = ",500-"; byteRangeSets = StringUtils.split(byteRangeSet, ','); System.out.println(Arrays.toString(byteRangeSets)); assertEquals(byteRangeSets.length, 1); byteRangeSet = "500-700,601-999,"; byteRangeSets = StringUtils.split(byteRangeSet, ','); assertEquals(byteRangeSets.length, 2); byteRangeSet = "500-700,,601-999,"; byteRangeSets = StringUtils.split(byteRangeSet, ','); assertEquals(byteRangeSets.length, 2); String tmp = "hello#$world#%test#$eee"; String[] tmps = StringUtils.splitByWholeSeparator(tmp, "#$"); System.out.println(Arrays.toString(tmps)); assertEquals(tmps.length, 3); tmp = "hello#$"; tmps = StringUtils.splitByWholeSeparator(tmp, "#$"); System.out.println(Arrays.toString(tmps)); assertEquals(tmps.length, 1); tmp = "#$hello#$"; tmps = StringUtils.splitByWholeSeparator(tmp, "#$"); System.out.println(Arrays.toString(tmps)); assertEquals(tmps.length, 1); tmp = "#$hello"; tmps = StringUtils.splitByWholeSeparator(tmp, "#$"); System.out.println(Arrays.toString(tmps)); assertEquals(tmps.length, 1); tmp = "#$hello#$world#$"; tmps = StringUtils.splitByWholeSeparator(tmp, "#$"); System.out.println(Arrays.toString(tmps)); assertEquals(tmps.length, 2); tmp = "#$hello#$#$world#$"; tmps = StringUtils.splitByWholeSeparator(tmp, "#$"); System.out.println(Arrays.toString(tmps)); assertEquals(tmps.length, 2); }
@Test void testSplit_String() { assertNull(StringUtils.split(null)); assertEquals(0, StringUtils.split("").length); String str = "a b .c"; String[] res = StringUtils.split(str); assertEquals(3, res.length); assertEquals("a", res[0]); assertEquals("b", res[1]); assertEquals(".c", res[2]); str = " a "; res = StringUtils.split(str); assertEquals(1, res.length); assertEquals("a", res[0]); str = "a" + WHITESPACE + "b" + NON_WHITESPACE + "c"; res = StringUtils.split(str); assertEquals(2, res.length); assertEquals("a", res[0]); assertEquals("b" + NON_WHITESPACE + "c", res[1]); }
@Test void testSplit_StringChar() { assertNull(StringUtils.split(null, '.')); assertEquals(0, StringUtils.split("", '.').length); String str = "a.b.. c"; String[] res = StringUtils.split(str, '.'); assertEquals(3, res.length); assertEquals("a", res[0]); assertEquals("b", res[1]); assertEquals(" c", res[2]); str = ".a."; res = StringUtils.split(str, '.'); assertEquals(1, res.length); assertEquals("a", res[0]); str = "a b c"; res = StringUtils.split(str, ' '); assertEquals(3, res.length); assertEquals("a", res[0]); assertEquals("b", res[1]); assertEquals("c", res[2]); }
@Test void testSplit_StringString_StringStringInt() { assertNull(StringUtils.split(null, ".")); assertNull(StringUtils.split(null, ".", 3)); assertEquals(0, StringUtils.split("", ".").length); assertEquals(0, StringUtils.split("", ".", 3).length); innerTestSplit('.', ".", ' '); innerTestSplit('.', ".", ','); innerTestSplit('.', ".,", 'x'); for (int i = 0; i < WHITESPACE.length(); i++) { for (int j = 0; j < NON_WHITESPACE.length(); j++) { innerTestSplit(WHITESPACE.charAt(i), null, NON_WHITESPACE.charAt(j)); innerTestSplit(WHITESPACE.charAt(i), String.valueOf(WHITESPACE.charAt(i)), NON_WHITESPACE.charAt(j)); } } String[] results; final String[] expectedResults = {"ab", "de fg"}; results = StringUtils.split("ab de fg", null, 2); assertEquals(expectedResults.length, results.length); for (int i = 0; i < expectedResults.length; i++) { assertEquals(expectedResults[i], results[i]); } final String[] expectedResults2 = {"ab", "cd:ef"}; results = StringUtils.split("ab:cd:ef", ":", 2); assertEquals(expectedResults2.length, results.length); for (int i = 0; i < expectedResults2.length; i++) { assertEquals(expectedResults2[i], results[i]); } }