@Test public void testSplitWithEscape() { String str = " , , a , b, c, d , , "; List<String> li = StringUtils.split(str, ',', '\\', false); assertTrue(Arrays.equals(new String[] { " ", " ", " a ", " b", " c", " d ", " ", " " }, li.toArray(new String[li.size()]))); li = StringUtils.split(str, ',', '\\', true); assertTrue( Arrays.equals(new String[] { "", "", "a", "b", "c", "d", "", "" }, li.toArray(new String[li.size()]))); str = " , , a , b, c, d , ,"; li = StringUtils.split(str, ',', '\\', false); assertTrue(Arrays.equals(new String[] { " ", " ", " a ", " b", " c", " d ", " ", "" }, li.toArray(new String[li.size()]))); li = StringUtils.split(str, ',', '\\', true); assertTrue( Arrays.equals(new String[] { "", "", "a", "b", "c", "d", "", "" }, li.toArray(new String[li.size()]))); str = "a , b\\,aobad, c,\n d"; li = StringUtils.split(str, ',', '\\', false); assertTrue(Arrays.equals(new String[] { "a ", " b,aobad", " c", "\n d" }, li.toArray(new String[li.size()]))); li = StringUtils.split(str, ',', '\\', true); assertTrue(Arrays.equals(new String[] { "a", "b,aobad", "c", "d" }, li.toArray(new String[li.size()]))); str = "a , b\\\\,aobad, c, d\\"; li = StringUtils.split(str, ',', '\\', false); assertTrue( Arrays.equals(new String[] { "a ", " b\\", "aobad", " c", " d\\" }, li.toArray(new String[li.size()]))); li = StringUtils.split(str, ',', '\\', true); assertTrue(Arrays.equals(new String[] { "a", "b\\", "aobad", "c", "d\\" }, li.toArray(new String[li.size()]))); str = "a | b\\|aobad| c|\n d"; li = StringUtils.split(str, '|', '\\', false); assertTrue(Arrays.equals(new String[] { "a ", " b|aobad", " c", "\n d" }, li.toArray(new String[li.size()]))); li = StringUtils.split(str, '|', '\\', true); assertTrue(Arrays.equals(new String[] { "a", "b|aobad", "c", "d" }, li.toArray(new String[li.size()]))); str = "a \\ b\\\\aobad\\ c\\\n d"; try { li = StringUtils.split(str, '\\', '\\', false); fail("Using the same character for escape and delimiter should not be possible"); } catch (IllegalArgumentException e) { } try { li = StringUtils.split(str, '\\', '\\', true); fail("Using the same character for escape and delimiter should not be possible"); } catch (IllegalArgumentException e) { } str = "foo\\\\bar"; li = StringUtils.split(str, ',', '\\', false); assertTrue(Arrays.equals(new String[] { "foo\\bar" }, li.toArray(new String[li.size()]))); str = "toto,foo\\\\bar"; li = StringUtils.split(str, ',', '\\', false); assertTrue(Arrays.equals(new String[] { "toto", "foo\\bar" }, li.toArray(new String[li.size()]))); str = "foo\\,bar"; li = StringUtils.split(str, ',', '\\', false); assertTrue(Arrays.equals(new String[] { "foo,bar" }, li.toArray(new String[li.size()]))); str = "foo\\,bar,foo\\\\bar"; li = StringUtils.split(str, ',', '\\', false); assertTrue(Arrays.equals(new String[] { "foo,bar", "foo\\bar" }, li.toArray(new String[li.size()]))); str = "foo\\\\\\,bar"; li = StringUtils.split(str, ',', '\\', false); assertTrue(Arrays.equals(new String[] { "foo\\,bar" }, li.toArray(new String[li.size()]))); str = "foo\\\\,bar"; li = StringUtils.split(str, ',', '\\', false); assertTrue(Arrays.equals(new String[] { "foo\\", "bar" }, li.toArray(new String[li.size()]))); str = "foo\\zbar"; li = StringUtils.split("foo\\zbar", ',', '\\', false); assertTrue(Arrays.equals(new String[] { "foo\\zbar" }, li.toArray(new String[li.size()]))); str = ""; li = StringUtils.split(str, ',', '\\', false); assertTrue(Arrays.equals(new String[] { "" }, li.toArray(new String[li.size()]))); str = "\\,"; li = StringUtils.split(str, ',', '\\', true); assertTrue(Arrays.equals(new String[] { "," }, li.toArray(new String[li.size()]))); str = "\\,a"; li = StringUtils.split(str, ',', '\\', true); assertTrue(Arrays.equals(new String[] { ",a" }, li.toArray(new String[li.size()]))); str = "a\\,"; li = StringUtils.split(str, ',', '\\', true); assertTrue(Arrays.equals(new String[] { "a," }, li.toArray(new String[li.size()]))); }
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);  }