1

Current value of the String is

^I am Shaikh with "([^"]*)" and "([^"]*)"$

My code is as below:

System.out.println(strAnnotationValue); // prints ^I am Shaikh with "([^"]*)" and "([^"]*)"$

strAnnotationValue = strAnnotationValue.replaceAll("\"", "\\\"");
System.out.println(strAnnotationValue); 

Actual Output:   ^I am Shaikh with "([^"]*)" and "([^"]*)"$
Expected Output: ^I am Shaikh with \"([^\"]*)\" and \"([^\"]*)\"$

I have written the proper code but it is not working, is there any other ways to do this?

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • 3
    Just a friendly notice: if the code you have doesn't work, there's a very good chance that the code you've written isn't correct. – Makoto May 26 '16 at 06:18
  • 1
    Take a look at this answers http://stackoverflow.com/questions/20556101/java-replace-all-in-a-string-with – Paco Abato May 26 '16 at 06:26

2 Answers2

1

If yours is a perfect code then you will get the expected output right??

Do one thing replace your this line

strAnnotationValue = strAnnotationValue.replaceAll("\"", "\\\"");

with this

strAnnotationValue = strAnnotationValue.replaceAll("\"", "\\\\\"");
Anoop LL
  • 1,548
  • 2
  • 21
  • 32
0

\\\" means \",when it output,it will be display: "

so do like this: \\\\"

\\\\" means \\",when it ouput, it will be diaplay:\"

strAnnotationValue = strAnnotationValue.replaceAll("\"", "\\\\\"");
star
  • 321
  • 2
  • 9