Skip to content

Instantly share code, notes, and snippets.

@tuanlongn
Last active August 1, 2021 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuanlongn/be166709c76629990948 to your computer and use it in GitHub Desktop.
Save tuanlongn/be166709c76629990948 to your computer and use it in GitHub Desktop.
Javascript: format string same as sprintf on C/PHP
// 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