Is there a sprintf equivalent for node.js

后端 未结 4 1317
忘了有多久
忘了有多久 2021-02-11 12:08

Looking to do output formatting (sprintf type functionality) in node.js, but before I write it myself I was wondering if there\'s something similar built-in (I\'ve trawled the d

相关标签:
4条回答
  • 2021-02-11 12:39

    console.log works fine.

    console.log('%d hours', 4); // 4 hours
    console.log('The %2$s contains %1$d monkeys', 4, 'tree'); // The tree contains 4 monkeys
    
    0 讨论(0)
  • 2021-02-11 12:40

    Here is the javascript version of sprintf:

    http://phpjs.org/functions/sprintf:522

    0 讨论(0)
  • 2021-02-11 12:43

    There is now printf-like support in util.format().

    Example:

    util.format('hello %s', 'world');
    // Returns: 'hello world'
    
    0 讨论(0)
  • 2021-02-11 12:43

    There are couple in the npm registry which are actual sprintf implementations since util.format has just a very basic support.

    • sprintf (deprecated now)
    • sprintf-js
    • fast-printf
    0 讨论(0)
提交回复
热议问题