adam bien's blog

Java 14: Text Blocks with String#formatted 📎

JEP-368 Text Blocks together with the method formatted in Java 14 deliver template-like functionality.

The following method:


@Test
public void interpolationWorkaround() {
    String html = """
        <html>
            <head>
                <title>%s</title>
            </head>
            <body>
            <h1>%s</h1>
            </body>
        </html>
        """.formatted("raw string", "hello, duke");
    System.out.println(html);
}

...generates the following output:


<html>
    <head>
        <title>raw string</title>
    </head>
    <body>
        <h1>hello, duke</h1>
    </body>
</html>