Skip to content
AgileLogics
3 min read

String Formatting

Prerequisites: Strings

String.format (and the equivalent System.out.printf) build a string from a template with % placeholders:

String name = "Ada";
int age = 36;
String message = String.format("%s is %d years old", name, age);
System.out.println(message); // Ada is 36 years old

System.out.printf("Pi is about %.2f%n", 3.14159); // Pi is about 3.14

Since Java 15, text blocks make multi-line strings easier to read:

String html = """
    <html>
        <body>Hi</body>
    </html>
    """;