Is there any way to use raw strings in Java (without escape sequences)?
(I\'m writing a fair amount of regex code and raw strings would make my code immensely more r
Have the raw text file in your class path and read it in with getResourceAsStream(....)
You could write your own, non-escaped property reader and put your strings in a resource file.
Yes.
Text Blocks Come to Java
Java 13 delivers long-awaited multiline strings
Some history: Raw String Literals were withdrawn. This was intended to be a preview language feature in JDK 12, but it was withdrawn and did not appear in JDK 12. It was superseded by Text Blocks (JEP 355) in JDK 13.
You can use text blocks to define multiline String literals with ease. You don’t need to add the visual clutter that comes with regular String literals: concatenation operators and escape sequences. You can also control how the String values are formatted. For example, let’s look at the following HTML snippet:
String html = """
<HTML>
<BODY>
<H1>"Java 13 is here!"</H1>
</BODY>
</HTML>""";
Notice the three quotation marks that delimit the beginning and ending of the block.
No. But there's an IntelliJ plug-in that makes this easier to deal with, called String Manipulation.
IntelliJ will also automatically escape a string pasted into it. (As @Dread points out, Eclipse has a plug-in to enable this.)
No, there isn't.
Generally, you would put raw strings and regexes in a properties file, but those have some escape sequence requirements too.
Note : As of today, not available. Probably I'll edit this answer again whenever the feature release.
There is an ongoing proposal to introduce Raw Strings in Java. They actually much useful in the cases of regex.
Example 1: A regular expression string that was coded as
System.out.println("this".matches("\\w\\w\\w\\w"));
may be alternately coded as
System.out.println("this".matches(`\w\w\w\w`));
since backslashes are not interpreted as having special meaning.
Example2 : A multi lines String literal with foreign language appends.
A multiple line string that was coded as
String html = "<html>\n" +
" <body>\n" +
" <p>Hello World.</p>\n" +
" </body>\n" +
"</html>\n";
may be alternately coded as
String html = `<html>
<body>
<p>Hello World.</p>
</body>
</html>
`;
which avoids the need for intermediate quotes, concatenation and explicit newlines.
Hopefully we can expect the release soon.