Skip to content

Instantly share code, notes, and snippets.

@kevinetienne
Created February 7, 2011 22:56
Show Gist options
  • Save kevinetienne/815458 to your computer and use it in GitHub Desktop.
Save kevinetienne/815458 to your computer and use it in GitHub Desktop.
String formatting for javascript
// thanks to http://stackoverflow.com/questions/610406/javascript-printf-string-format/3492815#3492815
// very similar to string.format:
// "{0} is dead, but {1} is alive!".format("ASP", "ASP.NET")
String.prototype.format = function() {
var formatted = this;
var args = Array.prototype.slice.call(arguments);
for(var i=0; args.length > i ;i++) {
formatted = formatted.replace("{" + i + "}", args[i]);
}
return formatted;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment