Node.js中util.format()-类似于printf格式字符串

util.format()方法是util模块的内置应用程序编程接口,类似于printf格式字符串,并使用第一个参数返回格式化的字符串。格式化的字符串包含零个或多个格式说明符,其中转换并替换了相应的参数值。它用作调试工具是一种同步方法,因此,它可能会占用可观的性能开销,从而可能阻塞事件循环

用法

util.format(format[, ...args])

参数:该方法接受上述和以下所述的两个参数:

  • 格式:由<string>类型的说明符组成,类似于printf格式字符串。

  • args:这是参数的<string>类型列表。

支持的说明符包括

  • %%:它用一个百分号('%%'/'%')替换说明符,即使提供了该参数也不会消耗任何参数。
  • %s (字符串):它将根据给定格式转换除Object,BigInt和-0之外的所有值。使用util.inspect()检查没有用户定义的toString函数的对象,并且BigInt值用n表示。
  • %c(CSS):如果传递了任何CSS,则会跳过该CSS。通常,此说明符被忽略。
  • %d (数字):它将根据给定格式转换除Symbol和BigInt之外的所有值。
  • %i (parseInt()):它解析一个<string>并返回一个整数,它用于除BigInt和Symbol之外的所有值。
  • %f (parseFloat()):它解析一个字符串并返回一个浮点数,它用于除Symbols之外的所有值。
  • %j (JSON):无需复杂的解析或翻译,如果参数中包含循环引用,则将其替换为字符串“ [Circular]”。
  • %o(对象):它是具有通用JavaScript对象格式的对象的字符串表示形式。与util.inspect()类似。它显示了完整的对象以及不可枚举的属性和代理。
  • %O(对象):与“%o”相似,但没有选项,它不包含不可枚举的属性和代理。

返回值

它返回<string>类型的格式化字符串

index.js 

// Node.js to demonstrate the 
// util.format() method  
  
// Import the util module  
const util = require('util'); 
  
function fun1() { 
    var val1 = util.format('%s:%s:%s', 'abc'); 
    // Returns:'foo:%s' 
  
    var val2 = util.format('%s:%s',  
        'abc', 'def', 'ghi', 'jkl'); 
    // Returns:'foo:bar baz' 
  
    var val3 = util.format(10, 20, 30); 
    // Returns:'1 2 3' 
  
    var val4 = util.format('%%:%s:%d'); 
    // Returns:'%% %s' 
  
    var val5 = util.format('%%:%s', 567); 
    // Returns:'%:567' 
  
    console.log(val1, '\n', val2, '\n',  
        val3, '\n', val4, '\n', val5); 
} 
  
// Function call 
fun1(); 

运行index.js

node index.js

输出
abc:
:abc:def ghi jkl
10 20 30
%%:%s:%d
%:567

index.js

// Node.js program to demonstrate 
// the util.format() method  
  
// Import the util module  
const util = require('util'); 
  
// Passing multiple values and 
// -0 on string specifier 
console.log("1.>", util.format( 
    '%%:%s', 'abc', 'def', -0)); 
  
// Passing multiple values  
console.log("2.>", util.format( 
    '%%', 'abc', 'def', 'ghi')); 
  
// Passing bigInt to string specifier 
console.log("3.>", util.format('%s',  
    'abc', 94321321321223372036854775807)); 
  
// Creating and passing Object along  
// with null prototype and a variable 
console.log("4.>", util.format('%s',  
        'abc', Object.create(null, 
        { [Symbol.toStringTag]:  
            { value:'def' } }))); 
  
// Passing string to Number specifier 
console.log("5.>", util.format('%d',  
            'abc', 94303685)); 
  
// Passing Symbol and Number to 
// parseInt specifier 
console.log("6.>", util.format( 
    '%i', '2020 year 2021, ', 'He was 40,'
    , '10.33, ', '10, ', 10)); 
  
// Passing string and Numbers 
// to parseFloat specifier 
console.log("7.>", util.format('%f',  
    '94321321321.564000 year 6546', 
    'abc', 943036854775807)); 
  
// Passing JSON string and Nunber 
// to JSON specifier 
console.log("8.>", util.format('%j', 
    '{ "name":"John", "age":31, "city":"New York" }',  
    'abc', 943036854775807)); 
  
// Passing class, string, and Number 
// to object specifier 
console.log("9.>", util.format('%o',  
    class Bar { }, 'abc', 943036854775807)); 
  
// Passing class, string, and Number 
// to Object specifier 
console.log("10.>", util.format('%o:%d', 
    class Foo { get [Symbol.toStringTag]()  
        { return 'abc'; } }, 
        'abc', 
        943036854775807 
)); 
  
// Random class 
class randomClass { } 
  
// Inspecting random class 
console.log("11.>",  
    util.inspect(new randomClass()));

运行index.js

node index.js

输出
1.> %:abc def -0
2.> % abc def ghi
3.> abc 9.432132132122338e+28
4.> abc [Object:null prototype] [def] {}
5.> NaN 94303685
6.> 2020 He was 40, 10.33, 10, 10
7.> 94321321321.564 abc 943036854775807
8.> "{ \"name\":\"John\", \"age\":31, 
    \"city\":\"New York\" }" abc 943036854775807
9.> <ref *1> [Function:Bar] {
    [length]:0,
    [prototype]:Bar { [constructor]:[Circular *1] },
    [name]:'Bar'
   } abc 943036854775807
10.> <ref *1> [Function:Foo] {
    [length]:0,
    [prototype]:Foo {
    [constructor]:[Circular *1],
    [Symbol(Symbol.toStringTag)]:[Getter]
    },
    [name]:'Foo'
   }:NaN 943036854775807
11.> randomClass {}

条件

  • 如果没有相应的参数传递给说明符,则不会替换它。
  • 如果传递的参数数量超过了指定符的数量,则多余的参数将被串联到返回的字符串中。
  • 如果‘values’不属于格式字符串,并且其类型不是字符串,则使用util.inspect()方法对其进行格式化。
  • 如果第一个参数没有有效的格式说明符,则util.format()返回串联的参数。

猜你喜欢

转载自blog.csdn.net/JackieDYH/article/details/124872193