how to replace “(double quotes) in a string with \\” in java

三世轮回 提交于 2019-11-30 06:51:34

You are looking for

strVar = strVar.replace("\"", "\\\"")

DEMO

I would avoid using replaceAll since it uses regex syntax in description of what to replace and how to replace, which means that \ will have to be escaped in string "\\" but also in regex \\ (needs to be written as "\\\\" string) which means that we would need to use

replaceAll("\"", "\\\\\"");

or probably little cleaner:

replaceAll("\"", Matcher.quoteReplacement("\\\""))

With replace we have escaping mechanism added automatically.

actually it is: strVar.replaceAll("\"", "\\\\\"");

Soumya Ranjan Sethy

For example take a string which has structure like this--->>>

String obj = "hello"How are"you";

And you want replace all double quote with blank value or in other word,if you want to trim all double quote.

Just do like this,

String new_obj= obj.replaceAll("\"", "");

Strings are formatted with double quotes. What you have is single quotes, used for chars. What you want is this:

String foo = " \"bar\" ";

This should give you what you want;

System.out.println("'\\\" value1 \\\"'");
Shobha

To replace double quotes

str=change condition to"or"      

str=str.replace("\"", "\\"");

After Replace:change condition to\"or\"

To replace single quotes

str=change condition to'or'      

str=str.replace("\'", "\\'");

After Replace:change condition to\'or\'

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!