Hifzur Rahman
Hifzur Rahman

Reputation: 103

Adding Escape parameter to a String is not working

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?

Upvotes: 1

Views: 62

Answers (2)

star
star

Reputation: 331

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

so do like this: \\\\"

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

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

Upvotes: 0

Anoop LL
Anoop LL

Reputation: 1575

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("\"", "\\\\\"");

Upvotes: 1

Related Questions