Java StringTemplate Tutorial

The StringTemplate class introduced in Java 21 is a powerful API for creating and manipulating string templates with placeholders. This tutorial will cover all the methods provided by the StringTemplate class, explain how they work, and provide examples to demonstrate their functionality.

Table of Contents

  1. Introduction
  2. Creating String Templates
    • of()
  3. Interpolating Values
    • interpolate()
  4. Retrieving Template Fragments
    • fragments()
  5. Combining Templates
    • combine()
  6. Accessing Values
    • values()
  7. Conclusion

Introduction

The StringTemplate class allows you to create templates with placeholders that can be dynamically replaced with actual values. This is useful for constructing dynamic strings, such as emails, SQL queries, or HTML content, based on templates.

Creating String Templates

of()

The of() method is used to create a StringTemplate from a string containing placeholders.

Syntax

public static StringTemplate of(String template)

Parameters:

  • template: The string containing the template with placeholders.

Example

import java.lang.StringTemplate;

public class StringTemplateOfExample {
    public static void main(String[] args) {
        StringTemplate template = StringTemplate.of("Hello, ${name}!");
        System.out.println(template);
    }
}

Output:

StringTemplate{template='Hello, ${name}!'}

Interpolating Values

interpolate()

The interpolate() method replaces the placeholders in the template with the specified values.

Syntax 1

public String interpolate()

Syntax 2

public String interpolate(Map<String, Object> values)

Parameters for interpolate(Map<String, Object> values):

  • values: A map of named parameters and their corresponding values to be inserted into the template.

Example

import java.lang.StringTemplate;
import java.util.Map;

public class StringTemplateInterpolateExample {
    public static void main(String[] args) {
        StringTemplate template = StringTemplate.of("Hello, ${name}!");
        String result = template.interpolate(Map.of("name", "Alice"));
        System.out.println(result);
    }
}

Output:

Hello, Alice!

Retrieving Template Fragments

fragments()

The fragments() method returns the fragments of a StringTemplate, which can include plain text and placeholders.

Syntax

public List<StringTemplate.Fragment> fragments()

Example

import java.lang.StringTemplate;
import java.util.List;

public class StringTemplateFragmentsExample {
    public static void main(String[] args) {
        StringTemplate template = StringTemplate.of("Hello, ${name}!");
        List<StringTemplate.Fragment> fragments = template.fragments();

        for (StringTemplate.Fragment fragment : fragments) {
            System.out.println("Fragment: " + fragment);
        }
    }
}

Output:

Fragment: Hello,
Fragment: ${name}
Fragment: !

Combining Templates

combine()

The combine() method combines multiple StringTemplate instances into a single StringTemplate.

Syntax

public static StringTemplate combine(StringTemplate... templates)

Parameters:

  • templates: A varargs parameter representing multiple StringTemplate instances to be combined.

Example

import java.lang.StringTemplate;

public class StringTemplateCombineExample {
    public static void main(String[] args) {
        StringTemplate template1 = StringTemplate.of("Hello, ");
        StringTemplate template2 = StringTemplate.of("${name}!");
        StringTemplate combined = StringTemplate.combine(template1, template2);

        String result = combined.interpolate(Map.of("name", "Alice"));
        System.out.println(result);
    }
}

Output:

Hello, Alice!

Accessing Values

values()

The values() method retrieves the values associated with the placeholders in a StringTemplate.

Syntax

public Map<String, Object> values()

Example

import java.lang.StringTemplate;
import java.util.Map;

public class StringTemplateValuesExample {
    public static void main(String[] args) {
        StringTemplate template = StringTemplate.of("Hello, ${name}! Today is ${day}.");

        Map<String, Object> values = Map.of(
            "name", "Alice",
            "day", "Monday"
        );

        String result = template.interpolate(values);
        Map<String, Object> templateValues = template.values();
        System.out.println("Template values: " + templateValues);
    }
}

Output:

Template values: {name=Alice, day=Monday}

Conclusion

The StringTemplate class in Java 21 provides a robust API for creating and manipulating string templates with placeholders. By using methods such as of(), interpolate(), fragments(), combine(), and values(), you can dynamically construct and manage complex strings in your applications. 

Whether you are generating emails, SQL queries, or HTML content, the StringTemplate class offers a flexible and powerful tool for template management.

Comments