util.format(format[, ...args])
util.format()
方法使用第一个参数作为类似 printf
的格式字符串(其可以包含零个或多个格式说明符)来返回格式化的字符串。每个说明符都替换为来自相应参数的转换后的值。支持的说明符有:
¥The util.format()
method returns a formatted string using the first argument
as a printf
-like format string which can contain zero or more format
specifiers. Each specifier is replaced with the converted value from the
corresponding argument. Supported specifiers are:
-
%s
:String
将用于转换除BigInt
、Object
和-0
之外的所有值。BigInt
值将用n
表示,没有用户定义的toString
函数的对象使用具有选项{ depth: 0, colors: false, compact: 3 }
的util.inspect()
进行检查。¥
%s
:String
will be used to convert all values exceptBigInt
,Object
and-0
.BigInt
values will be represented with ann
and Objects that have no user definedtoString
function are inspected usingutil.inspect()
with options{ depth: 0, colors: false, compact: 3 }
. -
%d
:Number
将用于转换除BigInt
和Symbol
之外的所有值。¥
%d
:Number
will be used to convert all values exceptBigInt
andSymbol
. -
%i
:parseInt(value, 10)
用于除BigInt
和Symbol
之外的所有值。¥
%i
:parseInt(value, 10)
is used for all values exceptBigInt
andSymbol
. -
%f
:parseFloat(value)
用于除Symbol
之外的所有值。¥
%f
:parseFloat(value)
is used for all values expectSymbol
. -
%j
:JSON.如果参数包含循环引用,则替换为字符串'[Circular]'
。¥
%j
: JSON. Replaced with the string'[Circular]'
if the argument contains circular references. -
%o
:Object
。具有通用 JavaScript 对象格式的对象的字符串表示形式。类似于具有选项{ showHidden: true, showProxy: true }
的util.inspect()
。这将显示完整的对象,包括不可枚举的属性和代理。¥
%o
:Object
. A string representation of an object with generic JavaScript object formatting. Similar toutil.inspect()
with options{ showHidden: true, showProxy: true }
. This will show the full object including non-enumerable properties and proxies. -
%O
:Object
。具有通用 JavaScript 对象格式的对象的字符串表示形式。类似于没有选项的util.inspect()
。这将显示完整的对象,但不包括不可枚举的属性和代理。¥
%O
:Object
. A string representation of an object with generic JavaScript object formatting. Similar toutil.inspect()
without options. This will show the full object not including non-enumerable properties and proxies. -
%c
:CSS
。此说明符被忽略,将跳过任何传入的 CSS。¥
%c
:CSS
. This specifier is ignored and will skip any CSS passed in. -
%%
:单个百分号 ('%'
)。这不消费参数。¥
%%
: single percent sign ('%'
). This does not consume an argument. -
返回:<string> 格式化的字符串
¥Returns: <string> The formatted string
如果说明符没有相应的参数,则不会替换它:
¥If a specifier does not have a corresponding argument, it is not replaced:
util.format('%s:%s', 'foo');
// Returns: 'foo:%s'
如果其类型不是 string
,则不属于格式字符串的值将使用 util.inspect()
进行格式化。
¥Values that are not part of the format string are formatted using
util.inspect()
if their type is not string
.
如果传给 util.format()
方法的参数多于说明符的数量,则额外的参数将以空格分隔串联到返回的字符串:
¥If there are more arguments passed to the util.format()
method than the
number of specifiers, the extra arguments are concatenated to the returned
string, separated by spaces:
util.format('%s:%s', 'foo', 'bar', 'baz');
// Returns: 'foo:bar baz'
如果第一个参数不包含有效的格式说明符,则 util.format()
返回以空格分隔的所有参数的串联的字符串:
¥If the first argument does not contain a valid format specifier, util.format()
returns a string that is the concatenation of all arguments separated by spaces:
util.format(1, 2, 3);
// Returns: '1 2 3'
如果只有一个参数传给 util.format()
,则它会按原样返回,不进行任何格式化:
¥If only one argument is passed to util.format()
, it is returned as it is
without any formatting:
util.format('%% %s');
// Returns: '%% %s'
util.format()
是同步的方法,旨在用作调试工具。某些输入值可能会产生显着的性能开销,从而阻塞事件循环。小心使用此函数,切勿在热代码路径中使用。
¥util.format()
is a synchronous method that is intended as a debugging tool.
Some input values can have a significant performance overhead that can block the
event loop. Use this function with care and never in a hot code path.