您好,欢迎访问一九零五行业门户网

JS字符串函数扩展代码_javascript技巧

复制代码 代码如下:
/****************************************************
*createby:joe zhou
*createdate:2011-9-4
*description:字符串辅助函数
****************************************************/
//string.prototype = {
// caption: function () {
// },
// leftpad: function (padchar, width) {
// if (this.length >= width) {
// return this;
// }
// }
//};
string.prototype.padleft = function (padchar, width) {
var ret = this;
while (ret.length if (ret.length + padchar.length ret = padchar + ret;
}
else {
ret = padchar.substring(0, width-ret.length) + ret;
}
}
return ret;
};
string.prototype.padright = function (padchar, width) {
var ret = this;
while (ret.length if (ret.length + padchar.length ret += padchar;
}
else {
ret += padchar.substring(0, width - ret.length);
}
}
return ret;
};
string.prototype.trim = function () {
return this.replace(/^\s+/, '').replace(/\s+$/, '');
};
string.prototype.trimleft = function () {
return this.replace(/^\s+/, '');
};
string.prototype.trimright = function () {
return this.replace(/\s+$/, '');
};
string.prototype.caption = function () {
if (this) {
return this.charat(0).touppercase() + this.substr(1);
}
return this;
};
string.prototype.reverse = function () {
var ret = '';
for (var i = this.length - 1; i >= 0; i--) {
ret += this.charat(i);
}
return ret;
};
string.prototype.startwith = function (comparevalue, ignorecase) {
if (ignorecase) {
return this.tolowercase().indexof(comparevalue.tolowercase()) == 0;
}
return this.indexof(comparevalue) == 0
};
string.prototype.endwith = function (comparevalue, ignorecase) {
if (ignorecase) {
return this.tolowercase().lastindexof(comparevalue.tolowercase()) == this.length - comparevalue.length;
}
return this.lastindexof(comparevalue) == this.length - comparevalue.length;
};
其它类似信息

推荐信息