Raw Strings in Java - for regex in particular. Multiline strings

前端 未结 12 1211
青春惊慌失措
青春惊慌失措 2020-12-01 02:13

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

相关标签:
12条回答
  • 2020-12-01 02:32

    Have the raw text file in your class path and read it in with getResourceAsStream(....)

    0 讨论(0)
  • 2020-12-01 02:32

    You could write your own, non-escaped property reader and put your strings in a resource file.

    0 讨论(0)
  • 2020-12-01 02:33

    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.

    0 讨论(0)
  • 2020-12-01 02:33

    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.)

    0 讨论(0)
  • 2020-12-01 02:34

    No, there isn't.

    Generally, you would put raw strings and regexes in a properties file, but those have some escape sequence requirements too.

    0 讨论(0)
  • 2020-12-01 02:39

    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.

    0 讨论(0)
提交回复
热议问题