printf()/format() for JavaScript String
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
/* | |
* 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