StringUtil { public static String escapeComma(final String str) { final StringBuilder escapedStr = new StringBuilder(str.length()); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (ESCAPE_CHAR == c) { escapedStr.append(ESCAPE_CHAR); escapedStr.append(REPLACEMENT_CHAR); } else if (COMMA == c) { escapedStr.append(ESCAPE_CHAR); escapedStr.append(ESCAPE_CHAR); } else { escapedStr.append(c); } } return escapedStr.toString(); } private  StringUtil(); static String toString(final byte[] bytes); static byte[] toBytes(final String string); static String escapeComma(final String str); static String unescapeComma(final String escapedStr); static byte[] toCsv(final Class<?>... classes); static Set<Class<? extends T>> csvToClasses(final byte[] bytes, final Class<? extends T> clazz); static String nullIfEmpty(final String string); static final char COMMA; }
@Test public void escapeCommaShouldReplaceBackslashWithSemiColon() { assertEquals("replaceWith\\;semicolon", StringUtil.escapeComma("replaceWith\\semicolon")); }
@Test public void escapeCommaShouldReplaceCommaWith2Backslashes() { assertEquals("replaceWith\\\\comma", StringUtil.escapeComma("replaceWith,comma")); }