Introduction to some javascript functions implemented by string.prototype

  • 2020-03-29 23:54:39
  • OfStack


//String. The prototype using    

//Batch replacement, such as: STR. ReplaceAll ([/ a/g/b/g/c/g], [" aaa ", "BBB", "CCC"])    
String.prototype.ReplaceAll=function (A,B) {   
    var C=this;   
    for(var i=0;i<A.length;i++) {   
        C=C.replace(A[i],B[i]);   
    };   
    return C;   
};   

//Remove the white space character & NBSP; from both ends of the character;  
String.prototype.Trim=function () {   
    return this.replace(/(^[/t/n/r]*)|([/t/n/r]*$)/g,'');   
};   

//Remove the white space character & NBSP; to the left of the character;  
String.prototype.LTrim=function () {   
    return this.replace(/^[/t/n/r]/g,'');   
};   

//Remove the white space character & NBSP; to the right of the character;  
String.prototype.RTrim=function () {   
    return this.replace(/[/t/n/r]*$/g,'');   
};   

//Returns the length of the character, one Chinese for two & PI;  
String.prototype.ChineseLength=function()   
{    
    return this.replace(/[^/x00-/xff]/g,"**").length;   
};   

//Determines whether the string ends with the specified string & PI;  
String.prototype.EndsWith=function (A,B) {   
    var C=this.length;   
    var D=A.length;   
    if(D>C)return false;   
    if(B) {   
        var E=new RegExp(A+'$','i');   
        return E.test(this);   
    }else return (D==0||this.substr(C-D,D)==A);   
};   
//Determines whether a string begins with the specified string & PI;  
String.prototype.StartsWith = function(str)    
{   
    return this.substr(0, str.length) == str;   
};   
//Where the string begins and how long is the character & PI taken out;  
String.prototype.Remove=function (A,B) {   
    var s='';   
    if(A>0)s=this.substring(0,A);   
    if(A+B<this.length)s+=this.substring(A+B,this.length);   
    return s;   
}; 

Related articles: