JS字符串函數(shù)擴(kuò)展代碼
更新時(shí)間:2011年09月13日 22:29:32 作者:
JS字符串函數(shù)擴(kuò)展代碼,大家可以參考下prototype的使用方法,擴(kuò)展自己的字符串處理函數(shù)。
復(fù)制代碼 代碼如下:
/****************************************************
*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腳本判斷手機(jī)瀏覽器類(lèi)型跳轉(zhuǎn)WAP手機(jī)網(wǎng)站
本文通過(guò)兩款js腳本判斷手機(jī)瀏覽器類(lèi)型跳轉(zhuǎn)到wap手機(jī)網(wǎng)站,感興趣的小伙伴快來(lái)學(xué)習(xí)吧2015-10-10
經(jīng)常用的圖片在容器中的水平垂直居中實(shí)例
經(jīng)常用的圖片在容器中的水平垂直居中實(shí)例...2007-06-06
解決layui動(dòng)態(tài)添加的元素click等事件觸發(fā)不了的問(wèn)題
今天小編就為大家分享一篇解決layui動(dòng)態(tài)添加的元素click等事件觸發(fā)不了的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
js實(shí)現(xiàn)select組件的選擇輸入過(guò)濾代碼
如何實(shí)現(xiàn)select組件的選擇輸入過(guò)濾作用,下面有一段js代碼,很實(shí)用,需要的朋友可以看看2014-10-10
require導(dǎo)入module.exports 或 exports導(dǎo)出的使用方法
module.exports用于導(dǎo)出整個(gè)模塊的內(nèi)容,可以通過(guò)賦值給 module.exports 導(dǎo)出一個(gè)對(duì)象、函數(shù)或值,導(dǎo)出的內(nèi)容可以被其他模塊通過(guò)require 導(dǎo)入,本文給大家介紹require導(dǎo)入module.exports 或 exports導(dǎo)出的使用,感興趣的朋友一起看看吧2023-11-11
JavaScript如何判斷input數(shù)據(jù)類(lèi)型
這篇文章主要介紹了JavaScript如何判斷input數(shù)據(jù)類(lèi)型,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Javascript取整函數(shù)及向零取整幾種常用的方法
這篇文章主要介紹了Javascript取整函數(shù)及向零取整幾種常用的方法,每種方法都有其特點(diǎn)和適用場(chǎng)景,推薦使用Math.trunc(),因?yàn)樗Z(yǔ)義明確、代碼易讀且性能較好,需要的朋友可以參考下2025-01-01
js代碼實(shí)現(xiàn)的加入收藏效果并兼容主流瀏覽器
這篇文章主要介紹了js代碼實(shí)現(xiàn)的加入收藏效果并兼容主流瀏覽器,需要的朋友可以參考下2014-06-06

