Class Fmt
p prints 0x and the reference bits of the argument; null is 0x0.
Grammar and presentation types follow
fmt 11.2 syntax:
replacement_field ::= "{" [arg_id] [":" format_spec] "}" and
format_spec ::= [[fill]align][sign]["#"]["0"][width]["." precision]["L"][type].
Implementation references:
format.h
(write_escaped_string at
L1763),
ranges.h,
chrono.h.
Null arguments (similar in spirit to SLF4J / typical Java logging): with no format spec,
values use a default string form (like String.valueOf(Object) for non-arrays); null
becomes "null" (no NPE). Arrays render as [1, 2, 3] with nesting, not [I@….
Radix: types b/B produce binary (optional alternate # → 0b/0B);
d/i, x, o use sign-and-magnitude for negatives ({:x} of
-42 is -2a); # adds 0x/0/0b after the sign.
Type S upper-cases a string. bits / Bf print IEEE-754 bit patterns (Java extras).
Debug (?): strings and characters are quoted and escaped as in fmt
write_escaped_string / write_escaped_char.
Ranges (arrays, Iterable, Map): empty spec follows fmt
(["a"], {'x'}, {"k": 1}); {::spec}, n, s/?s
follow
range_format_spec.
{::} uses an empty element spec (unquoted strings/chars).
Chrono: empty spec is fmt default (%F %T for date-times, %F for dates,
tick+unit for Duration). With a spec, [[fill]align][width][.precision]
then strftime (%Y, %F, glibc %-H/%_S/%OS, …) per
chrono_format_spec.
Duration also accepts time-of-day conversions and %Q/%q (Java Duration ticks:
seconds, or ms/us/ns when there is a fraction).
Sign (after ':', before type): + always; a single leading space flag for positives;
- default (only minus for negatives), same as omitting a sign flag.
Dynamic spec: nested braces in the part after ':' pull further arguments, e.g.
Fmt.format(Locale.US, "{:.{}f}", 3.14, 1) → "3.1" (decimal separator follows Locale).
Named fields ({name}) are supplied with arg(String, Object) in the varargs list.
Specs that are not a format_spec fall back to String.format(java.lang.String, java.lang.Object...)("%" + spec).
String overloads of format and formatTo reuse a compiled form for a pattern of
at most 256 characters. compile(CharSequence) always parses anew.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic NamedCreates a named argument that can be referenced from a template as{name}.static CompiledFormatcompile(CharSequence pattern) Parses a template and returns a reusable compiled representation.static Stringformat(CompiledFormat compiled, Object... args) Formats with a precompiled template using the current default locale.static Stringformat(CharSequence pattern, Object... args) Formats a template with positional and named arguments using the current default locale.static Stringformat(Locale locale, CompiledFormat compiled, Object... args) Formats with a precompiled template and an explicit locale.static Stringformat(Locale locale, CharSequence pattern, Object... args) Formats a template with an explicit locale.static voidformatTo(Appendable out, CompiledFormat compiled, Object... args) Formats with a precompiled template and appends output using the default locale.static voidformatTo(Appendable out, CharSequence pattern, Object... args) Formats and appends output to anAppendableusing the default locale.static voidformatTo(Appendable out, Locale locale, CompiledFormat compiled, Object... args) Formats with a precompiled template and appends output with an explicit locale.static voidformatTo(Appendable out, Locale locale, CharSequence pattern, Object... args) Formats and appends output to anAppendablewith an explicit locale.static voidformatTo(Consumer<? super String> sink, CompiledFormat compiled, Object... args) Formats with a precompiled template and passes output chunks to a string sink.static voidformatTo(Consumer<? super String> sink, CharSequence pattern, Object... args) Formats and passes result chunks to a string sink using the default locale.static voidFormats with a precompiled template and passes output chunks to a sink with explicit locale.static voidFormats and passes result chunks to a string sink with an explicit locale.static voidprint(CompiledFormat compiled, Object... args) Formats with a precompiled template and prints toSystem.outwithout newline.static voidprint(PrintStream out, CompiledFormat compiled, Object... args) Formats with a precompiled template and prints to aPrintStream.static voidprint(PrintStream out, CharSequence pattern, Object... args) Formats and prints to aPrintStreamwithout a trailing newline.static voidprint(PrintWriter out, CompiledFormat compiled, Object... args) Formats with a precompiled template and prints to aPrintWriter.static voidprint(PrintWriter out, CharSequence pattern, Object... args) Formats and prints to aPrintWriterwithout a trailing newline.static voidprint(CharSequence pattern, Object... args) Formats and prints toSystem.outwithout a trailing newline.static voidprintln(CompiledFormat compiled, Object... args) Formats with a precompiled template and prints toSystem.outwith newline.static voidprintln(PrintStream out, CompiledFormat compiled, Object... args) Formats with a precompiled template and prints to aPrintStreamwith newline.static voidprintln(PrintStream out, CharSequence pattern, Object... args) Formats and prints to aPrintStreamwith a trailing newline.static voidprintln(PrintWriter out, CompiledFormat compiled, Object... args) Formats with a precompiled template and prints to aPrintWriterwith newline.static voidprintln(PrintWriter out, CharSequence pattern, Object... args) Formats and prints to aPrintWriterwith a trailing newline.static voidprintln(CharSequence pattern, Object... args) Formats and prints toSystem.outwith a trailing newline.static voidsetDefaultLocale(Locale locale) Sets the locale used by overloads that do not accept a locale parameter.static StringConverts a value to text using the library formatting pipeline.
-
Constructor Details
-
Fmt
public Fmt()
-
-
Method Details
-
arg
Creates a named argument that can be referenced from a template as{name}.- Parameters:
name- argument name used in the templatevalue- argument value- Returns:
- immutable named argument wrapper
-
setDefaultLocale
Sets the locale used by overloads that do not accept a locale parameter.- Parameters:
locale- new default locale, must not benull
-
toString
Converts a value to text using the library formatting pipeline.- Parameters:
value- value to render- Returns:
- formatted value string
-
compile
Parses a template and returns a reusable compiled representation. Each call parsespatternagain and returns a new instance.- Parameters:
pattern- template text- Returns:
- compiled template
-
format
Formats a template with positional and named arguments using the current default locale.- Parameters:
pattern- template textargs- formatting arguments- Returns:
- rendered text
-
format
Formats a template with an explicit locale.- Parameters:
locale- locale for locale-sensitive conversionspattern- template textargs- formatting arguments- Returns:
- rendered text
-
format
Formats with a precompiled template using the current default locale.- Parameters:
compiled- precompiled templateargs- formatting arguments- Returns:
- rendered text
-
format
Formats with a precompiled template and an explicit locale.- Parameters:
locale- locale for locale-sensitive conversionscompiled- precompiled templateargs- formatting arguments- Returns:
- rendered text
-
print
Formats and prints toSystem.outwithout a trailing newline.- Parameters:
pattern- template textargs- formatting arguments
-
println
Formats and prints toSystem.outwith a trailing newline.- Parameters:
pattern- template textargs- formatting arguments
-
print
Formats with a precompiled template and prints toSystem.outwithout newline.- Parameters:
compiled- precompiled templateargs- formatting arguments
-
println
Formats with a precompiled template and prints toSystem.outwith newline.- Parameters:
compiled- precompiled templateargs- formatting arguments
-
print
Formats and prints to aPrintStreamwithout a trailing newline.- Parameters:
out- destination streampattern- template textargs- formatting arguments
-
println
Formats and prints to aPrintStreamwith a trailing newline.- Parameters:
out- destination streampattern- template textargs- formatting arguments
-
print
Formats with a precompiled template and prints to aPrintStream.- Parameters:
out- destination streamcompiled- precompiled templateargs- formatting arguments
-
println
Formats with a precompiled template and prints to aPrintStreamwith newline.- Parameters:
out- destination streamcompiled- precompiled templateargs- formatting arguments
-
print
Formats and prints to aPrintWriterwithout a trailing newline.- Parameters:
out- destination writerpattern- template textargs- formatting arguments
-
println
Formats and prints to aPrintWriterwith a trailing newline.- Parameters:
out- destination writerpattern- template textargs- formatting arguments
-
print
Formats with a precompiled template and prints to aPrintWriter.- Parameters:
out- destination writercompiled- precompiled templateargs- formatting arguments
-
println
Formats with a precompiled template and prints to aPrintWriterwith newline.- Parameters:
out- destination writercompiled- precompiled templateargs- formatting arguments
-
formatTo
Formats and appends output to anAppendableusing the default locale.- Parameters:
out- destination appendablepattern- template textargs- formatting arguments
-
formatTo
Formats and appends output to anAppendablewith an explicit locale.- Parameters:
out- destination appendablelocale- locale for locale-sensitive conversionspattern- template textargs- formatting arguments
-
formatTo
Formats with a precompiled template and appends output using the default locale.- Parameters:
out- destination appendablecompiled- precompiled templateargs- formatting arguments
-
formatTo
Formats with a precompiled template and appends output with an explicit locale.- Parameters:
out- destination appendablelocale- locale for locale-sensitive conversionscompiled- precompiled templateargs- formatting arguments
-
formatTo
Formats and passes result chunks to a string sink using the default locale.- Parameters:
sink- destination consumerpattern- template textargs- formatting arguments
-
formatTo
public static void formatTo(Consumer<? super String> sink, Locale locale, CharSequence pattern, Object... args) Formats and passes result chunks to a string sink with an explicit locale.- Parameters:
sink- destination consumerlocale- locale for locale-sensitive conversionspattern- template textargs- formatting arguments
-
formatTo
Formats with a precompiled template and passes output chunks to a string sink.- Parameters:
sink- destination consumercompiled- precompiled templateargs- formatting arguments
-
formatTo
public static void formatTo(Consumer<? super String> sink, Locale locale, CompiledFormat compiled, Object... args) Formats with a precompiled template and passes output chunks to a sink with explicit locale.- Parameters:
sink- destination consumerlocale- locale for locale-sensitive conversionscompiled- precompiled templateargs- formatting arguments
-