Created
February 7, 2011 22:56
-
-
Save kevinetienne/815458 to your computer and use it in GitHub Desktop.
String formatting for javascript
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
// 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