Last active
August 1, 2021 13:34
-
-
Save tuanlongn/be166709c76629990948 to your computer and use it in GitHub Desktop.
Javascript: format string same as sprintf on C/PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// format string same as sprintf on C/PHP | |
String.prototype.format = function() { | |
var str = this; | |
for (var i in arguments) { | |
str = str.replace('{' + i + '}', arguments[i]); | |
} | |
return str; | |
} | |
// usage: | |
'{0} is very {1}!'.format('Javascript', 'fun'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment