Skip to content

Instantly share code, notes, and snippets.

@fredrick
Created May 16, 2011 22:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredrick/975520 to your computer and use it in GitHub Desktop.
Save fredrick/975520 to your computer and use it in GitHub Desktop.
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