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

前端 未结 12 1254
青春惊慌失措
青春惊慌失措 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: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 = """
    
      
        

    "Java 13 is here!"

    """;

    Notice the three quotation marks that delimit the beginning and ending of the block.

提交回复
热议问题