• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Node.jsutil.format()-类似于printf格式字符串

武飞扬头像
JackieDYH
帮助1

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 

  1.  
    // Node.js to demonstrate the
  2.  
    // util.format() method
  3.  
     
  4.  
    // Import the util module
  5.  
    const util = require('util');
  6.  
     
  7.  
    function fun1() {
  8.  
    var val1 = util.format('%s:%s:%s', 'abc');
  9.  
    // Returns:'foo:%s'
  10.  
     
  11.  
    var val2 = util.format('%s:%s',
  12.  
    'abc', 'def', 'ghi', 'jkl');
  13.  
    // Returns:'foo:bar baz'
  14.  
     
  15.  
    var val3 = util.format(10, 20, 30);
  16.  
    // Returns:'1 2 3'
  17.  
     
  18.  
    var val4 = util.format('%%:%s:%d');
  19.  
    // Returns:'%% %s'
  20.  
     
  21.  
    var val5 = util.format('%%:%s', 567);
  22.  
    // Returns:'%:567'
  23.  
     
  24.  
    console.log(val1, '\n', val2, '\n',
  25.  
    val3, '\n', val4, '\n', val5);
  26.  
    }
  27.  
     
  28.  
    // Function call
  29.  
    fun1();

运行index.js

  1.  
    node index.js
  2.  
     
  3.  
    输出
  4.  
    abc:
  5.  
    :abc:def ghi jkl
  6.  
    10 20 30
  7.  
    %%:%s:%d
  8.  
    %:567

index.js

  1.  
    // Node.js program to demonstrate
  2.  
    // the util.format() method
  3.  
     
  4.  
    // Import the util module
  5.  
    const util = require('util');
  6.  
     
  7.  
    // Passing multiple values and
  8.  
    // -0 on string specifier
  9.  
    console.log("1.>", util.format(
  10.  
    '%%:%s', 'abc', 'def', -0));
  11.  
     
  12.  
    // Passing multiple values
  13.  
    console.log("2.>", util.format(
  14.  
    '%%', 'abc', 'def', 'ghi'));
  15.  
     
  16.  
    // Passing bigInt to string specifier
  17.  
    console.log("3.>", util.format('%s',
  18.  
    'abc', 94321321321223372036854775807));
  19.  
     
  20.  
    // Creating and passing Object along
  21.  
    // with null prototype and a variable
  22.  
    console.log("4.>", util.format('%s',
  23.  
    'abc', Object.create(null,
  24.  
    { [Symbol.toStringTag]:
  25.  
    { value:'def' } })));
  26.  
     
  27.  
    // Passing string to Number specifier
  28.  
    console.log("5.>", util.format('%d',
  29.  
    'abc', 94303685));
  30.  
     
  31.  
    // Passing Symbol and Number to
  32.  
    // parseInt specifier
  33.  
    console.log("6.>", util.format(
  34.  
    '%i', '2020 year 2021, ', 'He was 40,'
  35.  
    , '10.33, ', '10, ', 10));
  36.  
     
  37.  
    // Passing string and Numbers
  38.  
    // to parseFloat specifier
  39.  
    console.log("7.>", util.format('%f',
  40.  
    '94321321321.564000 year 6546',
  41.  
    'abc', 943036854775807));
  42.  
     
  43.  
    // Passing JSON string and Nunber
  44.  
    // to JSON specifier
  45.  
    console.log("8.>", util.format('%j',
  46.  
    '{ "name":"John", "age":31, "city":"New York" }',
  47.  
    'abc', 943036854775807));
  48.  
     
  49.  
    // Passing class, string, and Number
  50.  
    // to object specifier
  51.  
    console.log("9.>", util.format('%o',
  52.  
    class Bar { }, 'abc', 943036854775807));
  53.  
     
  54.  
    // Passing class, string, and Number
  55.  
    // to Object specifier
  56.  
    console.log("10.>", util.format('%o:%d',
  57.  
    class Foo { get [Symbol.toStringTag]()
  58.  
    { return 'abc'; } },
  59.  
    'abc',
  60.  
    943036854775807
  61.  
    ));
  62.  
     
  63.  
    // Random class
  64.  
    class randomClass { }
  65.  
     
  66.  
    // Inspecting random class
  67.  
    console.log("11.>",
  68.  
    util.inspect(new randomClass()));

运行index.js

  1.  
    node index.js
  2.  
     
  3.  
    输出
  4.  
    1.> %:abc def -0
  5.  
    2.> % abc def ghi
  6.  
    3.> abc 9.432132132122338e 28
  7.  
    4.> abc [Object:null prototype] [def] {}
  8.  
    5.> NaN 94303685
  9.  
    6.> 2020 He was 40, 10.33, 10, 10
  10.  
    7.> 94321321321.564 abc 943036854775807
  11.  
    8.> "{ \"name\":\"John\", \"age\":31,
  12.  
    \"city\":\"New York\" }" abc 943036854775807
  13.  
    9.> <ref *1> [Function:Bar] {
  14.  
    [length]:0,
  15.  
    [prototype]:Bar { [constructor]:[Circular *1] },
  16.  
    [name]:'Bar'
  17.  
    } abc 943036854775807
  18.  
    10.> <ref *1> [Function:Foo] {
  19.  
    [length]:0,
  20.  
    [prototype]:Foo {
  21.  
    [constructor]:[Circular *1],
  22.  
    [Symbol(Symbol.toStringTag)]:[Getter]
  23.  
    },
  24.  
    [name]:'Foo'
  25.  
    }:NaN 943036854775807
  26.  
    11.> randomClass {}

条件

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggafji
系列文章
更多 icon
同类精品
更多 icon
继续加载