zl程序教程

您现在的位置是:首页 >  后端

当前栏目

JS字符串函数扩展代码

JS扩展代码 函数 字符串
2023-06-13 09:14:30 时间
复制代码代码如下:

/****************************************************
*CreateBy:joezhou
*CreateDate:2011-9-4
*Description:字符串辅助函数
****************************************************/
//String.prototype={
//caption:function(){
//},
//leftPad:function(padChar,width){
//if(this.length>=width){
//returnthis;
//}
//}
//};
String.prototype.padLeft=function(padChar,width){
varret=this;
while(ret.length<width){
if(ret.length+padChar.length<width){
ret=padChar+ret;
}
else{
ret=padChar.substring(0,width-ret.length)+ret;
}
}
returnret;
};
String.prototype.padRight=function(padChar,width){
varret=this;
while(ret.length<width){
if(ret.length+padChar.length<width){
ret+=padChar;
}
else{
ret+=padChar.substring(0,width-ret.length);
}
}
returnret;
};
String.prototype.trim=function(){
returnthis.replace(/^\s+/,"").replace(/\s+$/,"");
};
String.prototype.trimLeft=function(){
returnthis.replace(/^\s+/,"");
};
String.prototype.trimRight=function(){
returnthis.replace(/\s+$/,"");
};
String.prototype.caption=function(){
if(this){
returnthis.charAt(0).toUpperCase()+this.substr(1);
}
returnthis;
};
String.prototype.reverse=function(){
varret="";
for(vari=this.length-1;i>=0;i--){
ret+=this.charAt(i);
}
returnret;
};
String.prototype.startWith=function(compareValue,ignoreCase){
if(ignoreCase){
returnthis.toLowerCase().indexOf(compareValue.toLowerCase())==0;
}
returnthis.indexOf(compareValue)==0
};
String.prototype.endWith=function(compareValue,ignoreCase){
if(ignoreCase){
returnthis.toLowerCase().lastIndexOf(compareValue.toLowerCase())==this.length-compareValue.length;
}
returnthis.lastIndexOf(compareValue)==this.length-compareValue.length;
};