Module java.base
Package java.util

Class FormatProcessor

java.lang.Object
java.util.FormatProcessor
All Implemented Interfaces:
ProcessorLinkagePREVIEW, StringProcessorPREVIEW, TemplateProcessorPREVIEW<String>, ValidatingProcessorPREVIEW<String,RuntimeException>

public final class FormatProcessor extends Object implements StringProcessorPREVIEW, ProcessorLinkagePREVIEW
FormatProcessor is a preview API of the Java platform.
Programs can only use FormatProcessor when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
This StringProcessorPREVIEW constructs a String result using Formatter specifications and values found in the StringTemplatePREVIEW. Unlike Formatter, FormatProcessorPREVIEW uses the value from the embedded expression that immediately follows, no whitespace, after the format specifier. For example:
FormatProcessor fmt = new FormatProcessor(Locale.ROOT);
int x = 10;
int y = 20;
String result = fmt."%05d\{x} + %05d\{y} = %05d\{x + y}";
In the above example, the value of result will be "00010 + 00020 = 00030".

Embedded expressions without a preceeding format specifier, use %s by default.

FormatProcessor fmt = new FormatProcessor(Locale.ROOT);
int x = 10;
int y = 20;
String result1 = fmt."\{x} + \{y} = \{x + y}";
String result2 = fmt."%s\{x} + %s\{y} = %s\{x + y}";
In the above example, the value of result1 and result2 will both be "10 + 20 = 30".

FormatProcessorPREVIEW format specification uses and exceptions are the same as those of Formatter.

However, there are two significant differences related to the position of arguments. An explict n$ and relative < index will cause an exception due to a missing argument list. Whitespace appearing between the specification and the embedded expression will also cause an exception.

FormatProcessorPREVIEW allows the use of different locales. For example:

Locale locale = Locale.forLanguageTag("th-TH-u-nu-thai");
FormatProcessor thaiFMT = new FormatProcessor(locale);
int x = 10;
int y = 20;
String result = thaiFMT."%d\{x} + %d\{y} = %d\{x + y}";
In the above example, the value of result will be "๑๐ + ๒๐ = ๓๐".

For day to day use, the predefined FMT FormatProcessorPREVIEW is available. FMT is defined using the Locale.ROOT. Example:

int x = 10;
int y = 20;
String result = FMT."0x%04x\{x} + 0x%04x\{y} = 0x%04x\{x + y}";
In the above example, the value of result will be "0x000a + 0x0014 = 0x001E".
Since:
21
See Also: