In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use the Text block in Java14, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
In Java, text blocks are multiline string text. This means that we don't need to get caught up in the confusion of explicit line Terminators, string concatenations, and delimiters, which are often used to write ordinary string text.
Java text blocks are available in Java 13 (JEP 355) and Java 14 (JEP 368) as preview functions. It is planned to be the standard function of Java 15 (JEP 378).
To enable this preview function, we must use the-enable-preview and-source 14 (two hyphens) flags.
Text block syntax
The text block contains multiple lines of text and uses three double quote characters ("") as its opening and closing delimiters.
The first three double quotation mark characters are always followed by a line Terminator.
We cannot include delimiters and text blocks on a line. The delimiter must be on its own line. Content can only start on the next line.
If the text content contains single or double quotation marks, you do not need to escape it.
String dbSchema = "" CREATE TABLE 'TEST'.'EMPLOYEE' (' ID' INT NOT NULL DEFAULT 0, 'FIRST_NAME' VARCHAR (100) NOT NULL,' LAST_NAME' VARCHAR (100) NULL 'STAT_CD' TINYINT NOT NULL DEFAULT 0) ""
Although the text block above looks very simple, there is still a lot going on behind the scenes. Let's explore.
Similar string
The instance generated from the text block is of type java.lang.String and has the same characteristics as a traditional double-quote string. This includes object representation and insert string pooling.
We can use a text block to pass as a method parameter of type String.
Text blocks can be used anywhere you can use string text. For example, we can use it for string concatenation.
String string = "Hello"; String textBlock = "World"; String joinedString = string + textBlock;System.out.println (joinedString)
Program output.
HelloWorld text block indentation
The text block retains the indentation of its contents, so let's refer to the first example.
String dbSchema = "" CREATE TABLE 'TEST'.'EMPLOYEE' (' ID' INT NOT NULL DEFAULT 0, 'FIRST_NAME' VARCHAR (100) NOT NULL,' LAST_NAME' VARCHAR (100) NULL 'STAT_CD' TINYINT NOT NULL DEFAULT 0) ""; System.out.println (dbSchema)
Program output:
| | CREATE TABLE 'TEST'.'EMPLOYEE' | (|' ID' INT NOT NULL DEFAULT 0, | 'FIRST_NAME' VARCHAR (100) NOT NULL, |' LAST_NAME' VARCHAR (100) NULL, | 'STAT_CD' TINYINT NOT NULL DEFAULT 0 |) |
Here, we have two types of indentation:
The first indent is the word "CREATE" from the beginning of the line to all lines. It may increase or decrease depending on a variety of factors, such as formatting plug-ins or developers' choices.
The second indentation begins with the character'(''to 'ID'.) In most cases, the indent time is 4 or 8 spaces.
Add some indentation
Suppose we want to add two tab indents to the left for all rows in the above example. To do this, we can carefully position the right three quotation marks to move the two tabs precisely to the left. The putter is in exactly the same position where it should start from its basic indent.
String dbSchema = "" CREATE TABLE 'TEST'.'EMPLOYEE' (' ID' INT NOT NULL DEFAULT 0, 'FIRST_NAME' VARCHAR (100) NOT NULL,' LAST_NAME' VARCHAR (100) NULL 'STAT_CD' TINYINT NOT NULL DEFAULT 0) "System.out.println (dbSchema)
Program output:
| | CREATE TABLE 'TEST'.'EMPLOYEE' | (|' ID' INT NOT NULL DEFAULT 0, | 'FIRST_NAME' VARCHAR (100) NOT NULL, |' LAST_NAME' VARCHAR (100) NULL, | 'STAT_CD' TINYINT NOT NULL DEFAULT 0 |) |
In addition, note that the Java compiler also removes trailing spaces on each line in the text block.
Line Terminator
Different platforms have different line Terminators. Java does not participate in platform detection, but standardizes all line Terminators in the text block to\ n.
If you need a platform line Terminator, you can use String::replaceAll ("\ n", System.lineSeparator ()).
String string = "Hello"; String textBlock = "World"; String joinedString = string + textBlock;joinedString = joinedString.replaceAll ("\ n", System.lineSeparator ()); System.out.println (joinedString); escape sequence
Most of the time, we just want to write content to multiple lines of the program, but in fact they are just a string of content. In this case, we can use a newline character, that is, a single backslash. It forbids the inclusion of implicit line breaks.
String dbSchema = "" CREATE TABLE 'TEST'.'EMPLOYEE'\ (\' ID' INT NOT NULL DEFAULT 0,\ 'FIRST_NAME' VARCHAR (100) NOT NULL,' LAST_NAME' VARCHAR (100) NULL,\ 'STAT_CD' TINYINT NOT NULL DEFAULT 0\); ""; System.out.println (dbSchema)
Program output:
| | CREATE TABLE TEST'.'EMPLOYEE' ('ID' INT NOT NULL DEFAULT 0,' FIRST_NAME' VARCHAR (100) NOT NULL, 'LAST_NAME' VARCHAR (100) NULL,' STAT_CD' TINYINT NOT NULL DEFAULT 0) |
If for some reason we don't want to delete indents, we can use'\ s'(ASCII character 32, spaces) to escape the sequence. Use it at the end of any line to ensure that the line will have all space characters until you encounter'\ s'.
String dbSchema = "" CREATE TABLE 'TEST'.'EMPLOYEE'\ s (\ s' ID' INT NOT NULL DEFAULT 0,\ s' FIRST_NAME' VARCHAR (100) NOT NULL,\ s' LAST_NAME' VARCHAR (100) NULL \ s' STAT_CD' TINYINT NOT NULL DEFAULT 0\ s) \ s ""; System.out.println (dbSchema.replaceAll ("\ s", "."))
Program output. In this program output, I replace all space characters with dots'.' Let you know its effect.
CREATE.TABLE.'TEST'.'EMPLOYEE'. (.. 'ID'.INT.NOT.NULL.DEFAULT.0.,.'FIRST_NAME'.VARCHAR (100) .NOT.null.,' LAST_NAME'.VARCHAR (100). NULL. . 'STAT_CD'.TINYINT.NOT.NULL.DEFAULT.0...) .. Thank you for reading this article carefully. I hope the article "how to use Text blocks in Java14" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.