JS字符串函數(shù)擴展代碼
更新時間:2011年09月13日 22:29:32 作者:
JS字符串函數(shù)擴展代碼,大家可以參考下prototype的使用方法,擴展自己的字符串處理函數(shù)。
復制代碼 代碼如下:
/****************************************************
*CreateBy:joe zhou
*CreateDate:2011-9-4
*Description:字符串輔助函數(shù)
****************************************************/
//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 < width) {
if (ret.length + padChar.length < width) {
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 < width) {
if (ret.length + padChar.length < width) {
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;
};
相關(guān)文章
兩款JS腳本判斷手機瀏覽器類型跳轉(zhuǎn)WAP手機網(wǎng)站
本文通過兩款js腳本判斷手機瀏覽器類型跳轉(zhuǎn)到wap手機網(wǎng)站,感興趣的小伙伴快來學習吧2015-10-10
解決layui動態(tài)添加的元素click等事件觸發(fā)不了的問題
今天小編就為大家分享一篇解決layui動態(tài)添加的元素click等事件觸發(fā)不了的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
require導入module.exports 或 exports導出的使用方法
module.exports用于導出整個模塊的內(nèi)容,可以通過賦值給 module.exports 導出一個對象、函數(shù)或值,導出的內(nèi)容可以被其他模塊通過require 導入,本文給大家介紹require導入module.exports 或 exports導出的使用,感興趣的朋友一起看看吧2023-11-11
JavaScript如何判斷input數(shù)據(jù)類型
這篇文章主要介紹了JavaScript如何判斷input數(shù)據(jù)類型,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-02-02
Javascript取整函數(shù)及向零取整幾種常用的方法
這篇文章主要介紹了Javascript取整函數(shù)及向零取整幾種常用的方法,每種方法都有其特點和適用場景,推薦使用Math.trunc(),因為它語義明確、代碼易讀且性能較好,需要的朋友可以參考下2025-01-01

