@Test public void testSplit() { String str; String[] ar; str = ",a ,,, b"; ar = StringUtils.split(str, ',', false); assertTrue(Arrays.equals(new String[] { "", "a ", "", "", " b" }, ar)); ar = StringUtils.split(str, ',', true); assertTrue(Arrays.equals(new String[] { "", "a", "", "", "b" }, ar)); str = "a , b, c,\n d"; ar = StringUtils.split(str, ',', false); assertTrue(Arrays.equals(new String[] { "a ", " b", " c", "\n d" }, ar)); ar = StringUtils.split(str, ',', true); assertTrue(Arrays.equals(new String[] { "a", "b", "c", "d" }, ar)); str = "a , b, c, d,"; ar = StringUtils.split(str, ',', false); assertTrue(Arrays.equals(new String[] { "a ", " b", " c", " d", "" }, ar)); ar = StringUtils.split(str, ',', true); assertTrue(Arrays.equals(new String[] { "a", "b", "c", "d", "" }, ar)); str = " , , a , b, c, d , , "; ar = StringUtils.split(str, ',', false); assertTrue(Arrays.equals(new String[] { " ", " ", " a ", " b", " c", " d ", " ", " " }, ar)); ar = StringUtils.split(str, ',', true); assertTrue(Arrays.equals(new String[] { "", "", "a", "b", "c", "d", "", "" }, ar)); }
public static List<String> split(String str, char delimiter, char escape, boolean trim) { if (delimiter == escape) { throw new IllegalArgumentException("Delimiter cannot be the escape character"); } List<String> ar = new ArrayList<>(); if (str.isEmpty()) { ar.add(str); return ar; } StringBuilder segment = new StringBuilder(); int i = 0; int length = str.length(); boolean lastCharDelimiter = false; while (i < length) { char c = str.charAt(i); if (c == escape) { if (i < length - 1) { char nextC = str.charAt(i + 1); if (nextC == delimiter || nextC == escape) { segment.append(nextC); i = i + 2; } else { segment.append(c); i++; } } else { segment.append(c); i++; } } else if (c == delimiter) { ar.add(trim ? segment.toString().trim() : segment.toString()); segment = new StringBuilder(); if (i == length - 1) { lastCharDelimiter = true; } i++; } else { segment.append(c); i++; } } if (segment.length() > 0 || lastCharDelimiter) { ar.add(trim ? segment.toString().trim() : segment.toString()); } return ar; }
StringUtils { public static List<String> split(String str, char delimiter, char escape, boolean trim) { if (delimiter == escape) { throw new IllegalArgumentException("Delimiter cannot be the escape character"); } List<String> ar = new ArrayList<>(); if (str.isEmpty()) { ar.add(str); return ar; } StringBuilder segment = new StringBuilder(); int i = 0; int length = str.length(); boolean lastCharDelimiter = false; while (i < length) { char c = str.charAt(i); if (c == escape) { if (i < length - 1) { char nextC = str.charAt(i + 1); if (nextC == delimiter || nextC == escape) { segment.append(nextC); i = i + 2; } else { segment.append(c); i++; } } else { segment.append(c); i++; } } else if (c == delimiter) { ar.add(trim ? segment.toString().trim() : segment.toString()); segment = new StringBuilder(); if (i == length - 1) { lastCharDelimiter = true; } i++; } else { segment.append(c); i++; } } if (segment.length() > 0 || lastCharDelimiter) { ar.add(trim ? segment.toString().trim() : segment.toString()); } return ar; } }
StringUtils { public static List<String> split(String str, char delimiter, char escape, boolean trim) { if (delimiter == escape) { throw new IllegalArgumentException("Delimiter cannot be the escape character"); } List<String> ar = new ArrayList<>(); if (str.isEmpty()) { ar.add(str); return ar; } StringBuilder segment = new StringBuilder(); int i = 0; int length = str.length(); boolean lastCharDelimiter = false; while (i < length) { char c = str.charAt(i); if (c == escape) { if (i < length - 1) { char nextC = str.charAt(i + 1); if (nextC == delimiter || nextC == escape) { segment.append(nextC); i = i + 2; } else { segment.append(c); i++; } } else { segment.append(c); i++; } } else if (c == delimiter) { ar.add(trim ? segment.toString().trim() : segment.toString()); segment = new StringBuilder(); if (i == length - 1) { lastCharDelimiter = true; } i++; } else { segment.append(c); i++; } } if (segment.length() > 0 || lastCharDelimiter) { ar.add(trim ? segment.toString().trim() : segment.toString()); } return ar; } private  StringUtils(); }
StringUtils { public static List<String> split(String str, char delimiter, char escape, boolean trim) { if (delimiter == escape) { throw new IllegalArgumentException("Delimiter cannot be the escape character"); } List<String> ar = new ArrayList<>(); if (str.isEmpty()) { ar.add(str); return ar; } StringBuilder segment = new StringBuilder(); int i = 0; int length = str.length(); boolean lastCharDelimiter = false; while (i < length) { char c = str.charAt(i); if (c == escape) { if (i < length - 1) { char nextC = str.charAt(i + 1); if (nextC == delimiter || nextC == escape) { segment.append(nextC); i = i + 2; } else { segment.append(c); i++; } } else { segment.append(c); i++; } } else if (c == delimiter) { ar.add(trim ? segment.toString().trim() : segment.toString()); segment = new StringBuilder(); if (i == length - 1) { lastCharDelimiter = true; } i++; } else { segment.append(c); i++; } } if (segment.length() > 0 || lastCharDelimiter) { ar.add(trim ? segment.toString().trim() : segment.toString()); } return ar; } private  StringUtils(); static String toAscii(String s, boolean normalize); static String toAscii(String s); static List<String> split(String str, char delimiter, char escape, boolean trim); static String[] split(String str, char delimiter, boolean trim); static String expandVars(String expression, Map<?, ?> properties); }
StringUtils { public static List<String> split(String str, char delimiter, char escape, boolean trim) { if (delimiter == escape) { throw new IllegalArgumentException("Delimiter cannot be the escape character"); } List<String> ar = new ArrayList<>(); if (str.isEmpty()) { ar.add(str); return ar; } StringBuilder segment = new StringBuilder(); int i = 0; int length = str.length(); boolean lastCharDelimiter = false; while (i < length) { char c = str.charAt(i); if (c == escape) { if (i < length - 1) { char nextC = str.charAt(i + 1); if (nextC == delimiter || nextC == escape) { segment.append(nextC); i = i + 2; } else { segment.append(c); i++; } } else { segment.append(c); i++; } } else if (c == delimiter) { ar.add(trim ? segment.toString().trim() : segment.toString()); segment = new StringBuilder(); if (i == length - 1) { lastCharDelimiter = true; } i++; } else { segment.append(c); i++; } } if (segment.length() > 0 || lastCharDelimiter) { ar.add(trim ? segment.toString().trim() : segment.toString()); } return ar; } private  StringUtils(); static String toAscii(String s, boolean normalize); static String toAscii(String s); static List<String> split(String str, char delimiter, char escape, boolean trim); static String[] split(String str, char delimiter, boolean trim); static String expandVars(String expression, Map<?, ?> properties);  }