Why does the following not work:
String test = "hello\"world".replaceAll("\"", "\\\"");
System.out.println(test);
What I'm trying to do is replace any occurrence of " with \".
So I want to get as output:
hello\"world
Regular expressions are overkill for this.
myString.replace("\"", "\\\"")
should do just fine and is more readable to someone familiar with the core libraries.
The replace method just replaces one substring with another.
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing
"aa"with"b"in the string"aaa"will result in"ba"rather than"ab".
". Try.replaceAll("\\"", "\\\"");