js简单函数封装

//每index个字符插入一个str字符串

String.prototype.insertStrPerIndex =function(index,str){

if(this.length>index&&index>0){
var arr = new Array();
var num = parseInt(this.length/index);
console.log(num);
for(var i=0;i<=num;i++){
var firstval =i*index;
if(firstval<this.length){
arr.push(this.substr(firstval,index));
}
}
return arr.join(str);
}else{
return this.toString();
}
}

//去掉字符串两端的空白

String.prototype.trim = function() {
return this.replace(/(^\s+)|(\s+$)/g, "");
}

//构建Stringbuffer

function StringBuffer(){ 
this.content = new Array; 
} 
StringBuffer.prototype.append = function( str ){ 
this.content.push( str ); 
} 
StringBuffer.prototype.toString = function(){ 
return this.content.join(""); 
}

 //去掉所有的空格、table

String.prototype.trimAll = function() {
    return this.replace(/\s/g,"")
}

 //重写replaceAll

String.prototype.replaceAll =function(oldWord,newWord){
        var res =this;
    var mid ="";
        while((mid=res.replace(oldWord,newWord))!=res){
            res =mid;
        }
        return res;
    }

 //判断是否是中文


 function ischinese(s){

              var ret=true;

              for(var i=0;i<s.length;i++)

              ret=ret && (s.charCodeAt(i)>=10000);

              return ret;

          }
 

 

posted on 2017-06-07 09:43  世界之大追梦者  阅读(314)  评论(0编辑  收藏  举报

导航