于是乎,自己动手写了个!!看到很多人都是用正则,咱不会,就用了最土的方法来实现了!帖上代码吧!希望对大家有所帮助!!!
复制代码 代码如下:
string.prototype.trimstart = function(trimstr){
if(!trimstr){return this;}
var temp = this;
while(true){
if(temp.substr(0,trimstr.length)!=trimstr){
break;
}
temp = temp.substr(trimstr.length);
}
return temp;
};
string.prototype.trimend = function(trimstr){
if(!trimstr){return this;}
var temp = this;
while(true){
if(temp.substr(temp.length-trimstr.length,trimstr.length)!=trimstr){
break;
}
temp = temp.substr(0,temp.length-trimstr.length);
}
return temp;
};
string.prototype.trim = function(trimstr){
var temp = trimstr;
if(!trimstr){temp= ;}
return this.trimstart(temp).trimend(temp);
};
用法大家应该明了吧!!!这里就不说了哈!!!有问题请指明!谢谢!