View source code
Display the source code in std/format/package.d from which this
page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a
Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Function std.format.format
Converts its arguments according to a format string into a string.
immutable(Char)[] format(Char, Args...)
(
in Char[] fmt,
Args args
)
if (isSomeChar!Char);
typeof(fmt) format(alias fmt, Args...)
(
Args args
)
if (isSomeString!(typeof(fmt)));
The second version of format
takes the format string as template
argument. In this case, it is checked for consistency at
compile-time and produces slightly faster code, because the length of
the output buffer can be estimated in advance.
Parameters
Name | Description |
---|---|
fmt | a format string |
args | a variadic list of arguments to be formatted |
Char | character type of fmt |
Args | a variadic list of types of the arguments |
Returns
The formatted string.
Throws
A FormatException
if formatting did not succeed.
See Also
sformat
for a variant, that tries to avoid garbage collection.
Example
writeln(format("Here are %d %s.", 3, "apples")); // "Here are 3 apples."
writeln("Increase: %7.2f %%" .format(17.4285)); // "Increase: 17.43 %"
Example
The format string can be checked at compile-time:
auto s = format!"%s is %s"("Pi", 3.14);
writeln(s); // "Pi is 3.14"
// This line doesn't compile, because 3.14 cannot be formatted with %d:
// s = format!"%s is %d"("Pi", 3.14);
Authors
Walter Bright, Andrei Alexandrescu, and Kenji Hara
License
Copyright © 1999-2024 by the D Language Foundation | Page generated by ddox.