Home >Web Front-end >JS Tutorial >Introduction to some javascript functions implemented by String.prototype_javascript skills

Introduction to some javascript functions implemented by String.prototype_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:13:021080browse
Copy code The code is as follows:

//String.prototype use

//Batch Replacement, for example: 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 C=C.replace(A[i],B[i] ); 
 }; 
 return C; .replace(/(^[/t/n/r]*)|([/t/n/r]*$)/g,'');
};

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

// Remove the blank characters on the right side of the character
String.prototype.RTrim=function () {
return this.replace(/[/t/n/r]*$/g,'') ;
};

// Return the length of characters, one Chinese character is counted as 2
String.prototype.ChineseLength=function()
{
return this.replace(/[ ^/x00-/xff]/g,"**").length;
};

// Determine whether the string ends with the specified string
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);
};
// Determine whether the string starts with the specified string
String.prototype.StartsWith = function(str)
{
return this.substr(0, str.length) == str; ';
if(A>0)s=this.substring(0,A);
if(A B return s;
};

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn