

JDK 12: Raw String Literals
source link: https://www.tuicool.com/articles/hit/7RFZBjv
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

JEP 326 Raw string literals provide a new way to declare strings without escape characters or unicode escapes. Let's take a look at the example below that declares multi-line strings with JDK 12 and prior releases.
To print all the days in a week with new lines, here is how we do it with the current JDK 11 release:
String daysInAWeekStr = "Days in a week \n" + "\t\t\tSunday\n" + "\t\t\tMonday\n" + "\t\t\tTuesday\n" + "\t\t\tWednesday\n" + "\t\t\tThursday\n" + "\t\t\tFriday\n" + "\t\t\tSaturday\n"; System.out.println(daysInAWeekStr);
This is what it will look like in JDK 12:
String daysInAWeekStr = `Days in a week Sunday Monday Tuesday Wednesday Thursday Friday Saturday`; System.out.println(daysInAWeekStr);
Notice " ` " characters to define the string variable. It avoids backslashes to escape unicode characters,\n new lines, and avoid "+" appending strings. It also increases the readability by avoiding leaning toothpick syndrome .
Output:
java --enable-preview <class> Days in a week Sunday Monday Tuesday Wednesday Thursday Friday Saturday
This feature is now (as of writing this blog) available as a preview feature in JDK 12 early access builds
. In order to enable this use, pass the --enable-preview
argument.
For more JDK 12 features scheduled in March 2019, click here . This list is not complete and more features might be added. We have to wait until Dec 2018 for the exhaustive list.
Recommend
-
108
-
77
-
35
-
49
-
11
Raw string literals in C# New in C# 11, there is a feature that I think will become highly popular. That feature is raw string literals. There have been a few different modifiers for string literals before now that work in v...
-
26
Microsoft unveils C# 11 list patterns, raw string literals Next version of Microsoft’s flagship programming language for .NET development will also simplify the synt...
-
18
C# 11 Preview Updates – Raw string literals, UTF-8 and more! Kathleen D April 18th, 2022 Features for C# 11 are coming al...
-
15
C Sharp 11 Raw String Literals posted Mon, Apr 25, 2022 by darren If you work with literal strings that can contain quotes or language strings such as JSON, XML, HTML, SQL or Reg...
-
17
Use Raw String Literals to generate TwiML in C# 11
-
26
Introducing C#11: Raw string literals 2022-11-29 by anthonygiretti Introduction
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK