在Java中转义双引号[duplicate]

时间:2022-09-15 13:07:24

Possible Duplicate:
In Java, is there a way to write a string literal without having to escape quotes?

可能的重复:在Java中,是否有一种方法可以在不需要转义引号的情况下写入字符串文字?

private static final String CREATE_TABLE_EXHIBITORS = "CREATE TABLE "users" ("_id" text PRIMARY KEY ,"name" text,"body" text,"image" text,"stand_id" text,"begin" long,"end" long,"changedate" long,"website" text,"facebook" text,"myspace" text,"twitter" text,"festivallink" text,"favorite" integer);";

Let's say I want this query to be a valid string in Java. Do I have to convert it to escape all double quotes with a trailing slash in front?

假设我希望这个查询在Java中是一个有效的字符串。我是否必须转换它来转义所有的双引号并在前面加上斜杠?

e.g.  = "CREATE TABLE \"users"\

Or is there a faster way to make this whole query a valid string at once? I thought you could use single quotes around the whole string to do that but that doesn't work either.

或者是否有更快的方法使整个查询成为有效的字符串?我认为你可以在整个字符串中使用单引号,但这也不行。

4 个解决方案

#1


42  

Escaping the double quotes with backslashes is the only way to do this in Java.

用反斜杠转义双引号是在Java中实现这一点的唯一方法。

Some IDEs around such as IntelliJ IDEA do this escaping automatically when pasting such a String into a String literal (i.e. between the double quotes surrounding a java String literal)

一些ide,比如IntelliJ IDEA,在将一个字符串粘贴到一个字符串文字时自动转义(例如在java字符串文字周围的双引号之间)

One other option would be to put the String into some kind of text file that you would then read at runtime

另一个选项是将字符串放入某种文本文件中,然后在运行时读取

#2


52  

Use Java's replaceAll(String regex, String replacement)

使用Java的replaceAll(字符串regex,字符串替换)

For example, Use a substitution char for the quotes and then replace that char with \"

例如,对引号使用替换字符,然后用\"替换该字符

String newstring = String.replaceAll("%","\"");

or replace all instances of \" with \\\"

或用“只\只\只\”替换所有的“只\只\只\只\只\”

String newstring = String.replaceAll("\"","\\\"");

#3


3  

For a String constant you have no choice other than escaping via backslash.

对于字符串常量,除了通过反斜线转义之外,您别无选择。

Maybe you find the MyBatis project interesting. It is a thin layer over JDBC where you can externalize your SQL queries in XML configuration files without the need to escape double quotes.

也许你觉得MyBatis项目很有意思。它是JDBC上的一层很薄的层,您可以在XML配置文件中具体化SQL查询,而不需要转义双引号。

#4


0  

Yes you will have to escape all double quotes by a backslash.

是的,你将不得不通过反斜杠来逃避所有双引号。

#1


42  

Escaping the double quotes with backslashes is the only way to do this in Java.

用反斜杠转义双引号是在Java中实现这一点的唯一方法。

Some IDEs around such as IntelliJ IDEA do this escaping automatically when pasting such a String into a String literal (i.e. between the double quotes surrounding a java String literal)

一些ide,比如IntelliJ IDEA,在将一个字符串粘贴到一个字符串文字时自动转义(例如在java字符串文字周围的双引号之间)

One other option would be to put the String into some kind of text file that you would then read at runtime

另一个选项是将字符串放入某种文本文件中,然后在运行时读取

#2


52  

Use Java's replaceAll(String regex, String replacement)

使用Java的replaceAll(字符串regex,字符串替换)

For example, Use a substitution char for the quotes and then replace that char with \"

例如,对引号使用替换字符,然后用\"替换该字符

String newstring = String.replaceAll("%","\"");

or replace all instances of \" with \\\"

或用“只\只\只\”替换所有的“只\只\只\只\只\”

String newstring = String.replaceAll("\"","\\\"");

#3


3  

For a String constant you have no choice other than escaping via backslash.

对于字符串常量,除了通过反斜线转义之外,您别无选择。

Maybe you find the MyBatis project interesting. It is a thin layer over JDBC where you can externalize your SQL queries in XML configuration files without the need to escape double quotes.

也许你觉得MyBatis项目很有意思。它是JDBC上的一层很薄的层,您可以在XML配置文件中具体化SQL查询,而不需要转义双引号。

#4


0  

Yes you will have to escape all double quotes by a backslash.

是的,你将不得不通过反斜杠来逃避所有双引号。