format

Converts its arguments according to a format string into a string.

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.

  1. immutable(Char)[] format(Char[] fmt, Args args)
    immutable(Char)[]
    format
    (
    Char
    Args...
    )
    (
    in Char[] fmt
    ,
    Args args
    )
    if ()
  2. typeof(fmt) format(Args args)

Parameters

fmt Char[]
args Args

a variadic list of arguments to be formatted

Char

character type of fmt

Args

a variadic list of types of the arguments

Return Value

Type: immutable(Char)[]

The formatted string.

Throws

A FormatException if formatting did not succeed.

Examples

assert(format("Here are %d %s.", 3, "apples") == "Here are 3 apples.");

assert("Increase: %7.2f %%".format(17.4285) == "Increase:   17.43 %");

The format string can be checked at compile-time:

auto s = format!"%s is %s"("Pi", 3.14);
assert(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);

See Also

sformat for a variant, that tries to avoid garbage collection.

Meta