Escape double quotes in Java

Possible duplicate:
In Java, is there a way to write a string literal without the need for quotes?

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);"; 

Say I want this query to be a valid string in Java. Do I have to convert it to avoid all double quotes with a trailing slash in front?

 eg = "CREATE TABLE \"users"\ 

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

+48
java escaping
Jun 08 '11 at 11:21
source share
4 answers

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

Some IDEs, such as IntelliJ IDEA , are automatically executed when such a string is inserted into a string literal (i.e. between double quotes surrounding the java string literal)

Another option is to put String in some kind of text file that you would read at runtime

+40
Jun 08 2018-11-11T00:
source share

Use Java replaceAll(String regex, String replacement)

For example, use the char substitution for quotation marks, and then replace the char with \"

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

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

 String newstring = String.replaceAll("\"","\\\""); 
+47
Jun 08 '11 at 11:25
source share

For the String constant, you have no choice but to escape through a backslash.

You might be interested in the MyBatis project. This is a thin layer on top of JDBC where you can exteriorize your SQL queries in XML configuration files without the need to avoid double quotes.

+2
Jun 08 '11 at 11:33
source share

Yes, you have to avoid all double quotes with backslashes.

0
Jun 08 2018-11-11T00:
source share



All Articles