Some of the ones collected from Wuyou Script are really good and practical and worth collecting.
Methods to extend the prototype object of Array
//Delete data in the array
Array.prototype.del = function(n)
{
if (n<0) return this;
return this.slice(0,n ).concat(this.slice(n 1,this.length));
}
// Array shuffling
Array.prototype.random = function()
{
var nr =[], me=this, t;
while(me.length>0)
{
nr[nr.length] = me[t = Math.floor(Math.random() * me .length)];
me = me.del(t);
}
return nr;
}
//Number array sorting
Array.prototype.sortNum = function( f)
{
if (!f) f=0;
if (f==1) return this.sort(function(a,b){return b-a;});
return this.sort(function(a,b){return a-b;});
}
// Get the maximum item of the numeric array
Array.prototype.getMax = function()
{
return this.sortNum(1)[0];
}
// Get the minimum item of the numeric array
Array.prototype.getMin = function()
{
return this.sortNum (0)[0];
}
// The position where the specified element value first appears in the array
Array.prototype.indexOf = function(o)
{
for (var i =0; ireturn -1;
}
// Remove duplicate items in the array
Array.prototype.removeRepeat=function()
{
this.sort();
var rs = [];
var cr = false;
for (var i=0; i< ;this.length; i )
{
if (!cr) cr = this[i];
else if (cr==this[i]) rs[rs.length] = i;
else cr = this[i];
}
var re = this;
for (var i=rs.length-1; i>=0; i--) re = re.del (rs[i]);
return re;
}
Example:
var arr=["ni","wo","ta"];
Delete "wo" in the array
var newArr=arr.del(1);
Return the position where "me" first appears in the array, if not, return -1
var strPos=arr .indexOf("me");
Methods that extend the prototype object of String
//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; ireturn 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 left blank
String.prototype.LTrim = function()
{
return this.replace(/^s /g,"");
}
// Remove the right blank
String.prototype.RTrim = function()
{
return this. replace(/s $/g,"");
}
// Remove whitespace 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;ire = re.replace(q1[i],q2[i]);
return re;
}
//Unicode conversion
String.prototype.ascW = function()
{
var strText = "";
for (var i=0; ireturn strText;
}