If I had a string variable:
String example = \"Hello, I\'m here\";
and I wanted to add an escape character in front of every \'
\'
Try Apache Commons Text library-
System.out.println(StringEscapeUtils.escapeCsv("a\",")); System.out.println(StringEscapeUtils.escapeJson("a\",")); System.out.println(StringEscapeUtils.escapeEcmaScript("Hello, I'm \"here"));
Result:
"a""," a\", Hello, I\'m \"here