node.js 是否有等效的 sprintf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2697899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a sprintf equivalent for node.js
提问by Steven Mohapi-Banks
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 docs to no avail) or if someone's already written a module.
希望在 node.js 中进行输出格式化(sprintf 类型功能),但在我自己编写它之前,我想知道是否有类似的内置内容(我已经搜索了文档无济于事)或者是否有人已经编写了一个模块。
Many thanks
非常感谢
回答by lapo
There is now printf-like support in util.format().
中现在有printf类似的支持util.format()。
Example:
例子:
util.format('hello %s', 'world');
// Returns: 'hello world'
回答by KARASZI István
There are couple in the npm registry which are actual sprintfimplementations since util.formathas just a very basic support.
npm 注册表中有几个是实际sprintf实现,因为util.format只有非常基本的支持。
sprintf(deprecated now)- sprintf-js
冲刺(现已弃用)- sprintf-js
回答by Sarfraz
Here is the javascript version of sprintf:
这是 javascript 版本sprintf:
回答by Jürgen Paul
console.logworks fine.
console.log工作正常。
console.log('%d hours', 4); // 4 hours
console.log('The %2$s contains %1$d monkeys', 4, 'tree'); // The tree contains 4 monkeys

