56

Possible Duplicate:
In Java, is there a way to write a string literal without having to escape 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);";

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?

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.

Community
  • 1
  • 1
Vincent
  • 6,058
  • 15
  • 52
  • 94
  • 4
    wouldn't single quotes in this sql statement be valid anyway? no escaping required in that case... – Gryphius Jun 08 '11 at 11:24
  • 1
    Strings surrounded by Single quotes in SQL are string literals, i.e. data, whereas Strings surrounded by double quotes are identifiers, i.e. Table names column names etc. Double quotes around column names are only necessary for column names that are otherwise ambiguous with SQL keywords such as DATE, NUMBER, COMMENT etc. – BertNase Jun 08 '11 at 11:27
  • What IDE are you using ? For example in eclipse you can change a setting to automatically escape string literals. – Mike Kwan Jun 08 '11 at 11:37
  • [This answer](http://stackoverflow.com/a/121513/403455) shows how to paste multi-line escaped strings in Eclispe. – Jeff Axelrod Mar 09 '12 at 18:42
  • Note (Jan. 2018), raw string literals might be coming for Java (JDK 10 or more): see [In Java, is there a way to write a string literal without having to escape quotes?](https://stackoverflow.com/a/48481601/6309). – VonC Jan 27 '18 at 23:26

4 Answers4

58

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

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("\"","\\\"");
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Stas Jaro
  • 4,747
  • 5
  • 31
  • 53
45

Escaping the double quotes with backslashes is the only way to do this in 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)

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

informatik01
  • 16,038
  • 10
  • 74
  • 104
BertNase
  • 2,374
  • 20
  • 24
2

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.

vanje
  • 10,180
  • 2
  • 31
  • 47
0

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

greydet
  • 5,509
  • 3
  • 31
  • 51