This is driving me nuts. I believe I asked this exact same question, but I can’t find it any more (I used Stack Overflow search, Google Search, manually searched my posts, and searched my code).
I wanted something that would be like the C# String.Format where you could do something like
string format = String.Format("Hi {0}",name);
just for JavaScript of course and one person gave me a simple answer it was not like a jQuery plugin or anything, but I think you made some JSON thing or something, and it worked and was simple to use.
I for the life of me can’t find this post.
I do have this in my code, but I can’t seem to find anything that uses it and I am pretty sure I used it a couple of times:
String.prototype.format = function(o)
{
return this.replace(/{([^{}]*)}/g,
function(a, b)
{
var r = o[b];
return typeof r === 'string' ? r : a;
}
);
};
Adapt the code from MsAjax string.
Just remove all of the
_validateParamscode and you are most of the way to a full fledged .NET string class in JavaScript.Okay, I liberated the msajax string class, removing all the msajax dependencies. It Works great, just like the .NET string class, including trim functions, endsWith/startsWith, etc.
P.S. – I left all of the Visual Studio JavaScript IntelliSense helpers and XmlDocs in place. They are innocuous if you don’t use Visual Studio, but you can remove them if you like.
String.js