Home >Web Front-end >JS Tutorial >javascript String extension method collection_javascript skills

javascript String extension method collection_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:04:14882browse

//Get the character array
String.prototype.ToCharArray=function()
{
return this.split("");
}
//Get N identical strings
String.prototype.Repeat=function(num)
{
var tmpArr=[];
for(var i=0;i return tmpArr.join("");
}
//Reverse order
String.prototype.Reverse=function()
{
return this.split("").reverse( ).join("");
}
//Test whether it is a number
String.prototype.IsNumeric=function()
{
var tmpFloat=parseFloat(this);
if(isNaN(tmpFloat)) return false;
var tmpLen=this.length-tmpFloat.toString().length;
return tmpFloat "0".Repeat(tmpLen)==this;
}
//Test whether it is an integer
String.prototype.IsInt=function()
{
if(this=="NaN") return false;
return this==parseInt(this ).toString();
}
// Combine multiple blanks into one blank
String.prototype.resetBlank = function()
{
return this.replace(/s /g ," ");
}
//Remove the left margin
String.prototype.LTrim = function()
{
return this.replace(/^s /g,"") ;
}
//Remove the right margin
String.prototype.RTrim = function()
{
return this.replace(/s $/g,"");
}
// Remove the blanks on both sides
String.prototype.trim = function()
{
return this.replace(/(^s )|(s $)/g,"");
}
// Reserved numbers
String.prototype.getNum = function()
{
return this.replace(/[^d]/g,"");
}
//Reserved letters
String.prototype.getEn = function()
{
return this.replace(/[^A-Za-z]/g,"");
}
//Keep Chinese
String.prototype.getCn = function()
{
return this.replace(/[^u4e00-u9fa5uf900-ufa2d]/g,"");
}
//Get the byte length
String.prototype.getRealLength = function()
{
return this.replace(/[^x00-xff]/g,"--" ).length;
}
//Truncate a string of specified length from the left
String.prototype.left = function(n)
{
return this.slice(0,n) ;
}
//Truncate a string of specified length from the right
String.prototype.right = function(n)
{
return this.slice(this.length-n);
}
//HTML Encoding
String.prototype.HTMLEncode = function()
{
var re = this;
var q1 = [/x26/g,/x3C/ g,/x3E/g,/x20/g];
var q2 = ["&",""," "];
for(var i=0;i re = re.replace(q1[i],q2[i]);
return re;
}
//Unicode conversion
String.prototype.ascW = function( )
{
var strText = "";
for (var i=0; i return strText;
}

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