Maybe most annoying thing in JavaScript is lack of string functions. Yes, there is functions but they are basic... give me char, replace it ... but first thing that came up to my mind was "Hey, it would be nice to have printf(...) function in JavaScript!". And next thing was ... you are guessing... googling :).
But that is few line of code, and for those of you who are used to C#, but not just for you ;), here is the snippet that can be helpful :
String.prototype.format = function(){
result = this;
for( var i = 0; i < arguments.length; i++ ) {
result = result.replace("{" + i + "}", arguments[i]);
}
return result;
};
And finally, how to use it:
"Hello, {0}, this is {1} formated message. See you later, {0}!".format("Peter", "JavaScript");
No comments:
Post a Comment