13

My string has double quotes in it, in C# I would do:

string blah = @"this is my ""text";

how would I do that in Java?

codaddict
  • 445,704
  • 82
  • 492
  • 529
mrblah
  • 99,669
  • 140
  • 310
  • 420
  • 1
    possible duplicate of [Java equivalent of C#'s verbatim strings with @](http://stackoverflow.com/questions/2673855/java-equivalent-of-cs-verbatim-strings-with) – mmmmmm Mar 31 '12 at 19:39
  • 2
    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:27

3 Answers3

14

No. Such a feature is not available in Java.
From the Sun docs:

When an escape sequence is encountered in a print statement, the compiler interprets it accordingly. For example, if you want to put quotes within quotes you must use the escape sequence, \", on the interior quotes. To print the sentence

She said "Hello!" to me.

you would write

System.out.println("She said \"Hello!\" to me.");
codaddict
  • 445,704
  • 82
  • 492
  • 529
4

There is nothing like it currently, but it is a "Fix understood" request for enhancement.

If added, this feature would allow such string literals:

String qry = @"
   SELECT
     a.name, b.number
   FROM
     User a, Data b
   WHERE
     a.name = "James"
      AND
     a.id = b.id
";

Some of the comments reject this principally on the fact that it's a syntactic sugar, but obviously there's high demand for it, so it's possible that we'll see this someday.

polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • 6
    I don't understand the reason to refuse a feature because it is syntactic sugar. Allowing coders not to write boiler code seems to me like a language improvement... – jeremy-george Apr 18 '11 at 15:46
  • 2
    The bugs.sun.com link in this answer is broken currently; it links to an Oracle page with the message "This bug is not available." Is a current version of this page available somewhere? – Jon Schneider Oct 30 '13 at 16:30
2

Yes* (since September 2019) but it used different sintax. “”” (three double-quote marks).

*In more recent java versions (+13 in preview, +15 as production ready), mostly equivalent can be achieved with the java text blocks.

String html = """         
            <xml>
                <ody>
                    <pan>example xml </pan>
                </ody>
            </xml>""";

https://docs.oracle.com/en/java/javase/13/text_blocks/index.html

João
  • 2,296
  • 5
  • 20
  • 30