absl::StrFormat

Returns a string given a printf()-style format string and zero or more additional arguments.

Synopsis

Declared in <absl/strings/str_format.h>

template<typename... Args>
[[nodiscard]]
std::string
StrFormat(
    FormatSpec<Args...> const& format,
    Args const&... args);

Description

Use it as you would sprintf(). StrFormat() is the primary formatting function within the str_format library, and should be used in most cases where you need type-safe conversion of types into formatted strings.

The format string generally consists of ordinary character data along with one or more format conversion specifiers (denoted by the % character). Ordinary character data is returned unchanged into the result string, while each conversion specification performs a type substitution from StrFormat()'s other arguments. See the comments for FormatSpec for full information on the makeup of this format string.

Example:

std::string s = absl::StrFormat( "Welcome to %s, Number %d!", "The Village", 6); EXPECT_EQ("Welcome to The Village, Number 6!", s);

Returns an empty string in case of error.

Return Value

The formatted string, or an empty string on error.

NOTE

The return value should not be discarded.

Parameters

NameDescription
formatThe format string.
argsThe arguments substituted into the format string.