How do I string.format() or sprintf() in coffeescript?
相关问题
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- google-drive can't get push notifications
- How to reimport module with ES6 import
- Why is `node.js` dying when called from inside pyt
- How to verify laravel passport api token in node /
相关文章
- node连接远程oracle报错
- How can make folder with Firebase Cloud Functions
- @angular-cli install fails with deprecated request
- node.js modify file data stream?
- How to resolve hostname to an ip address in node j
- Transactionally writing files in Node.js
- Log to node console or debug during webpack build
- Get file created date in node
The idiomatic version of the accepted answer:
So there's 2 things going on here. First is interpolation, which coffeescript directly supports using double-quoted string literals and ruby style syntax like this:
That will replace the placeholders with the corresponding variables from the local scope. That's the idiomatic way to handle string interpolation in coffeescript (and ruby).
Second is the formatting, which you should probably handle separately if you want to get numbers with specific decimal places, thousands separate with commas, leading zeros, or that sort of thing. However, CoffeeScript can interpolate the formatting as well, so you could do
For other features with C-style formatters, have a look at JavaScript sprintf (which I found on this answer)
This seems to do the trick:
Translated using some javascript from fearphage
Which can then be used like this:
mystring would then be:
Using the #{var} approach (while perfect for example given) doesn't work with a string that needs to be recycled several times. In a looping situation for example: