Skip to content

Instantly share code, notes, and snippets.

@fredrick
Created May 16, 2011 22:28
Embed
What would you like to do?
printf()/format() for JavaScript String
/*
* Extend JavaScript String object with printf() like functionality
* "{0} is dead, but {1} is alive!".format("This", "that");
*/
String.prototype.format = function() {
var formatted = this;
for(arg in arguments) {
formatted = formatted.replace("{" + arg + "}", arguments[arg]);
}
return formatted;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment