Time for a quick inspirational solution to have something similar in JavaScript as string.Format("Hello {0} {1}!", "Daniel", "Wertheim");
in C#.
String.prototype.apply = String.prototype.apply || function () {
var arg,
regExp,
v = this.valueOf();
if (!v)
return;
for (var i = 0, m = arguments.length; i < m; i++) {
arg = arguments[i];
if (arg === undefined)
arg = null;
regExp = new RegExp('\{' + i + '\}', 'gm');
v = v.replace(regExp, arg);
}
return v;
};
This can now easily be used like this:
'Hello {0} {1}!'.apply("Daniel", "Wertheim");
Works for me, hope it’s to any help. Please feel free to suggest improvements.
//Daniel