Possible Duplicate:
JavaScript equivalent to print开发者_JS百科f/string.format
Do we have something like C# String.Format(...) in JavaScript?
I like to be able to say String.Format('text text {0}, text text {1}', value1, value2);
and ideally as an extension method:
'text text {0}, text text {1}'.format(value1, value2);
Thanks,
here is your solution:
String.format = function() {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
s = s.replace(reg, arguments[i + 1]);
}
return s;
}
more informations here => Equivalent of String.format in jQuery
精彩评论