/ Published in: JavaScript
Expand |
Embed | Plain Text
function _StringFormatInline() { var txt = this; for(var i=0;i<arguments.length;i++) { var exp = new RegExp('\\{' + (i) + '\\}','gm'); txt = txt.replace(exp,arguments[i]); } return txt; } function _StringFormatStatic() { for(var i=1;i<arguments.length;i++) { var exp = new RegExp('\\{' + (i-1) + '\\}','gm'); arguments[0] = arguments[0].replace(exp,arguments[i]); } return arguments[0]; } if(!String.prototype.format) { String.prototype.format = _StringFormatInline; } if(!String.format) { String.format = _StringFormatStatic; } //Static var str = String.format("This is a {0} string using the {1} method.","formatted","static"); //Inline var str = "This is a {0} string using the {1} method.".format("formatted","inline");
You need to login to post a comment.
