Skip to content

Instantly share code, notes, and snippets.

@mloza
Last active October 30, 2019 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mloza/d21b5af3580f1317ea79f81790659d3d to your computer and use it in GitHub Desktop.
Save mloza/d21b5af3580f1317ea79f81790659d3d to your computer and use it in GitHub Desktop.
public class TextBlocksConcatenation {
public static void main(String[] args) {
String title = "Hello title";
String header = "Hello header";
System.out.println("""
<html>
<head>
<title>""" + title + """
</title>
</head>
<body>
<h1>""" + header + """
</h1>
</body>
</html>
""");
}
}
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>13</release>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
package pl.mloza;
public class TextBlocksFormatting {
private static String indentation = """
<html>
<head>
<title>%s</title>
</head>
<body>
<h1>%s</h1>
</body>
</html>
""";
public static void main(String[] args) {
System.out.println(indentation.formatted("Hello title", "Hello header"));
}
}
public class TextBlocksIndentation {
// #1
private static String indentation = """
<html>
<head>
<title>Hello world!</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
""";
// #2
private static String indentationTwo = """
<html>
<head>
<title>Hello world!</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
""";
public static void main(String[] args) {
System.out.println(indentation);
System.out.println(indentationTwo);
}
}
public class TextBlocksInvalid {
// #1
private static String invalidOne = """""";
// #2
private static String invalidTwo = """ """;
// #3
private static String invalidThree = """
";
// #4
private static String invalidFour = """
asd \ asd
"""";
}
package pl.mloza;
public class TextBlocks {
private static String classic = " <html>\n" +
" <head>\n" +
" <title>Hello world!</title>\n" +
" </head>\n" +
" <body>\n" +
" <h1>Hello World</h1>\n" +
" </body>\n" +
" </html>";
private static String block = """
<html>
<head>
<title>Hello world!</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
""";
public static void main(String[] args) {
System.out.println(classic);
System.out.println(block);
}
}
public class TextBlocksTwo {
// #1
private static String noLineEnding = """
<html>
<head>
<title>Hello world!</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>""";
// #2
private static String block = """
"We can put here quotes but slashes needs to be escaped \\"
Comments are not working here # // /* xxx */
Triple quotes needs to be escaped \"""
""";
// #3
private static String emptyString = """
""";
// #4
private static String blockConcatenation = block + noLineEnding;
// #5
private static String escapeSequences = """
First line \n Second line
""";
public static void main(String[] args) {
System.out.println(noLineEnding);
System.out.println(block);
System.out.println("Empty string: " + emptyString);
System.out.println(blockConcatenation);
System.out.println(escapeSequences);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment