Module java.base
Package java.lang

Interface StringTemplate


public interfaceStringTemplate
StringTemplate is a preview API of the Java platform.
Programs can only useStringTemplate when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
StringTemplatePREVIEW is the run-time representation of a string template or text block template in a template expression.

In the source code of a Java program, a string template or text block template contains an interleaved succession offragment literals andembedded expressions. Thefragments() method returns the fragment literals, and thevalues() method returns the results of evaluating the embedded expressions.StringTemplatePREVIEW does not provide access to the source code of the embedded expressions themselves; it is not a compile-time representation of a string template or text block template.

StringTemplatePREVIEW is primarily used in conjunction with a template processor to produce a string or other meaningful value. Evaluation of a template expression first produces an instance ofStringTemplatePREVIEW, representing the right hand side of the template expression, and then passes the instance to the template processor given by the template expression.

For example, the following code contains a template expression that uses the template processorRAW, which simply yields theStringTemplatePREVIEW passed to it:

int x = 10;int y = 20;StringTemplate st = RAW."\{x} + \{y} = \{x + y}";List<String> fragments = st.fragments();List<Object> values = st.values();
fragments will be equivalent toList.of("", " + ", " = ", ""), which includes the empty first and last fragments.values will be the equivalent ofList.of(10, 20, 30).

The following code contains a template expression with the same template but with a different template processor,STR:

int x = 10;int y = 20;String s = STR."\{x} + \{y} = \{x + y}";
When the template expression is evaluated, an instance ofStringTemplatePREVIEW is produced that returns the same lists fromfragments() andvalues() as shown above. TheSTR template processor uses these lists to yield an interpolated string. The value ofs will be equivalent to"10 + 20 = 30".

Theinterpolate() method provides a direct way to perform string interpolation of aStringTemplatePREVIEW. Template processors can use the following code pattern:

List<String> fragments = st.fragments();List<Object> values    = st.values();... check or manipulate the fragments and/or values ...String result = StringTemplate.interpolate(fragments, values);
Theprocess(Processor) method, in conjunction with theRAW processor, may be used to defer processing of aStringTemplatePREVIEW.
StringTemplate st = RAW."\{x} + \{y} = \{x + y}";...other steps...String result = st.process(STR);
The factory methodsof(String) andof(List, List) can be used to construct aStringTemplatePREVIEW.

Implementation Note:
Implementations ofStringTemplatePREVIEW must minimally implement the methodsfragments() andvalues(). Instances ofStringTemplatePREVIEW are considered immutable. To preserve the semantics of string templates and text block templates, the list returned byfragments() must be one element larger than the list returned byvalues().
SeeJava Language Specification:
15.8.6 Process Template Expressions
Since:
21
See Also: