如何用java中的“\”替换字符串中的“(双引号)”

时间:2022-03-12 05:45:24

I have string variable strVar with value as ' "value1" ' and i want to replace all the double quotes in the value with ' \" '. So after replacement value would look like ' \"value1\" '

我有string变量strVar,值为' value1 ',我想用' \ '替换值中的所有双引号。所以替换后的值会变成' \ ' value1\ ' '

How to do this in java? Kindly help me.

如何在java中实现这一点?请帮助我。

6 个解决方案

#1


30  

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因为它使用正则表达式语法的描述来取代以及如何取代,这意味着\必须转义字符串“\ \”,但也在正则表达式\ \(需要写成“\ \ \ \”字符串)这意味着我们将需要使用

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

or probably little cleaner:

或者小清洁:

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

With replace we have escaping mechanism added automatically.

有了替换,我们就有了自动添加的机制。

#2


5  

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

实际上是:strVar。replaceAll(“\””、“\ \ \ \ \ " ");

#3


3  

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

#4


0  

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

字符串用双引号进行格式化。你所拥有的是单引号,用于chars。你想要的是:

String foo = " \"bar\" ";

字符串foo = " \"bar\";

#5


0  

This should give you what you want;

这会给你想要的;

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

#6


0  

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\'

后替换:改变条件\”或\”

#1


30  

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因为它使用正则表达式语法的描述来取代以及如何取代,这意味着\必须转义字符串“\ \”,但也在正则表达式\ \(需要写成“\ \ \ \”字符串)这意味着我们将需要使用

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

or probably little cleaner:

或者小清洁:

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

With replace we have escaping mechanism added automatically.

有了替换,我们就有了自动添加的机制。

#2


5  

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

实际上是:strVar。replaceAll(“\””、“\ \ \ \ \ " ");

#3


3  

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

#4


0  

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

字符串用双引号进行格式化。你所拥有的是单引号,用于chars。你想要的是:

String foo = " \"bar\" ";

字符串foo = " \"bar\";

#5


0  

This should give you what you want;

这会给你想要的;

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

#6


0  

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\'

后替换:改变条件\”或\”