string String.prototype.format(params object[])

实现这个方法,是因为我太习惯cSharp里的String.Format方法。这个自然没有它功能强大,仅仅实现按照参数和格式序号简单替换。

String.prototype.format = function(){
	if(arguments.length == 0) return this;
	var _this = this, i = 0, matches = _this.match(/{\d+}/g), l = Math.min(arguments.length, matches.length), _i, _s;
	if(matches.length == 0){
		_this = null; i = null; matches = null; l = null; _i = null; _s = null;
		return this;
	}
	for(; i < l; i++)
		if(!isNaN(_i = parseInt((_s = matches[i] + '').substr(1))))
			_this = _this.replace(_s, arguments[_i] + '');
	i = null; matches = null; l = null; _i = null; _s = null;
	return _this;
}
posted @ 2010-04-04 22:41  MKing's Kindom  阅读(779)  评论(0编辑  收藏  举报