108

Quick question. Is there an equivalent of @ as applied to strings in Java:

For example I can do @"c:\afolder\afile" in C# and have it ignore the escape characters when processing instead of having to do "c:\\afolder\\aFile". Is there a Java equivalent?

hmmm: stackoverflow is escaping on me .. lol. The second example should read:

c:(double-backslash)afolder(double-backslash)aFile

falstro
  • 34,597
  • 9
  • 72
  • 86
Simon Rigby
  • 1,786
  • 4
  • 17
  • 28
  • 3
    Possible duplicate of: http://stackoverflow.com/questions/2018556/does-java-have-the-character-to-escape-string-quotes/2018583#2018583 – codaddict Apr 20 '10 at 09:13
  • 2
    Took the liberty of clarifying the title since `@` can be used to use language keywords as identifiers as well. – Joey Apr 20 '10 at 09:15
  • @unicornaddict - yup indeed its the same question. Apologies didn't see that when searching. @ Johannes - good move .. thanks – Simon Rigby Apr 20 '10 at 09:22
  • If you want to do just that in Windows, and you use Windows XP or newer (and you should!), then you can just do File file = new File("C:/afolder/afile"); On the other hand, if you want to be more portable, you should look at @CPerkins' excellent answer. – Haakon Løtveit Dec 06 '16 at 13:22
  • 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

5 Answers5

81

No. Escaping / externalizing the string is your only choice.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
35

No, Java doesn't have verbatim string literals.

If you want a Java-like (and Java-VM-based) language that does, however, you might want to look at Groovy which has various forms of string literal.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • First link (verbatim string literals) points to _"Visual Studio 2003 Retired Technical documentation"_ `groovy.codehaus.org` links are broken – Vladas Maier Oct 22 '18 at 09:30
  • 1
    @ace: Updated to the current "string" article, which includes a section on verbatim string literals. Also updated Groovy links. – Jon Skeet Oct 22 '18 at 09:35
17

As Kent and Jon have said, no there isn't.

I'm answering just to point out that even if there were, for your particular case, it would be a bad idea in the general case, assuming a more than one-off program.

Java programs run on more platforms than just Windows, and other platforms have different file delimiters. So instead of dealing with escaped backslashes, the correct way to handle your particular example is by getting the file separator property:


    String sep = System.getProperty("file.separator");
    String filename = ROOTDIR + sep + "folder" + sep + "afile";

Where you'd have separately created ROOTDIR based on some policy - not only the platform, but whether you want your "afile" to be relative to the actual file system root, or relative to the user's home directory.

But definitely, using the file separator property makes your programs more widely usable. Is it more work? Yes. As Wanda Sykes says, "But it's worth it".

CPerkins
  • 8,968
  • 3
  • 34
  • 47
  • 1
    Hiya and thanks. the fact that its a 'filename' in my case in kind of irrelevant. All my code is doing is taking a string (or trying to) and passing it on. I don't actually do anything with it other than pass it on to a web service. I was testing the response from a c# WCF service being called from Java. As I was just hard coding a test it was at this point that I discovered this limitation. I;m not actually doing anything 'file based' with the string. – Simon Rigby Apr 21 '10 at 08:57
  • 2
    Verbatim strings are great for regex patterns. – Gordon Bean Mar 08 '19 at 18:54
12

Currently it is not supported in Java but could be available in future releases. There was created JEP 326: Raw String Literals at 2018/01/23

See the progress at https://bugs.openjdk.java.net/browse/JDK-8196004

Probably some day you will be able to do it with:

`c:\afolder\afile`

UPDATE: JEP proposed to drop from JDK 12:326: Raw String Literals (Preview) You can read the rationale here: http://mail.openjdk.java.net/pipermail/jdk-dev/2018-December/002402.html

And more details here https://bugs.openjdk.java.net/browse/JDK-8215682

The bottom line: There will not be verbatim strings in Java in near future. And even if it will appear it rather will not be ``.

engilyin
  • 1,011
  • 9
  • 22
  • 1
    Seeing as this is one of the most popular discussion on the matter, [JEP 326](http://openjdk.java.net/jeps/326) have been withdrawn. The discussion was [restarted in Jan 2019](https://mail.openjdk.java.net/pipermail/amber-spec-experts/2019-January/000931.html), and is now tracked in [JEP 355](https://openjdk.java.net/jeps/355) for a JDK13 release. – NPras Jul 23 '19 at 00:49
9

Java has it 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 java text blocks.

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

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

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