NodeJS String format like Python?

泪湿孤枕 提交于 2019-12-08 17:05:24

问题


In python, I can do the following:

name = "bob"

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

Is there anything similar to that (or Python's .format()) in JavaScript/NodeJS?


回答1:


sprintf should do what you are asking for I think.




回答2:


You can use util.format, it's printf like function.




回答3:


Another option is template strings

For example:

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


来源:https://stackoverflow.com/questions/10788408/nodejs-string-format-like-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!