NodeJS 字符串格式,如 Python

NodeJS String format like Python?

本文关键字:Python 格式 字符串 NodeJS      更新时间:2023-09-26

在python中,我可以执行以下操作:

name = "bob"
print("Hey, %s!" % name)

在JavaScript/NodeJS中是否有类似于(或Python的.format())的东西?

你可以使用util.format,它printf像函数。

另一个选项是模板字符串

例如:

const name = "bob";
console.log(`Hey, ${name}!`);

我认为sprintf应该做你所要求的。