Extension function for
string object:
String.prototype.trim = function () {return this.replace (/^\s+|\s+$/g, "");
} String.prototype.ltrim = function () {return this.replace (/^\s+/g, "");
} String.prototype.rtrim = function () {return this.replace (/\s+$/g, "");
} String.prototype.splitAndTrim = function ($delimiter, $limit) {var $ss = This.split ($delimiter, $limit);
for (var $i =0; $i < $ss. length; $i + +) $ss [$i] = $ss [$i].trim ();
return $SS; } String.prototype.htmlEntities = function () {return this.replace (/&/g, ' & '). Replace (/</g, ' < '). Replace
(/>/g, ' > ');
} String.prototype.stripTags = function () {return this.replace (/< ([^>]+) >/g, ');
} String.prototype.toArray = function () {return this.split (');
} String.prototype.toIntArray = function () {var returnarray = [];
for (var i=0; i<this.length; i++) {Returnarray.push (This.charcodeat (i));
return returnarray; } String.prototype.replaceAll = function ($old, $snew) {return thiS.replace (New RegExp ($old, "GM"), $snew);
}
Variable substitution
var a = "I Love {0}" and "Love {1},where are {0}!"; A.format ("You", "Me");
String.prototype.format = function () {
var args = arguments;
Return This.replace (/\{(\d+) \}/g,function (m,i,o,n) {return
args[i];
}
Append string at end of string
String.prototype.append = function ($str) {return
this.concat ($STR);
}
Deletes the character at the specified index location, and the index is invalid without deleting any characters
String.prototype.deleteCharAt = function ($sIndex) {
if ($sIndex <0 | | | $sIndex >=this.length) {return
This.valueof ();
} else if ($sIndex ==0) {return
this.substring (1,this.length);
} else if ($sIndex ==this.length-1) {return
this.substring (0,this.length-1);
} else{return
this.substring (0, $sIndex) +this.substring ($sIndex + 1);
}
Deletes a string between the specified indexes. The characters $sIndex and $eindex are not deleted! Reliance on Deletecharat
String.prototype.deleteString = function ($sIndex, $eIndex) {
if ($sIndex = = $eIndex) {return
This.deletecharat ($sIndex);
} else{
if ($sIndex > $eIndex) {
var tindex= $eIndex;
$eIndex = $sIndex;
$sIndex =tindex;
}
if ($sIndex <0) $sIndex =0;
if ($eIndex >this.length-1) $eIndex =this.length-1;
Return this.substring (0, $sIndex + 1) +this.substring ($eIndex, this.length);
}
Checks whether a string ends with a string (str)
String.prototype.endsWith = function ($str) {return
this.substr (this.length-$str. Length) = = $str;
}
Checks whether the string starts with a string
String.prototype.startsWith = function (str) {return
this.substr (0, str.length) = = str;
}
Compares two strings for equality and is case-insensitive!
String.prototype.equalsIgnoreCase = function ($str) {
if (this.length!= $str. Length) {return
false;
} else{
var tmp1=this.tolowercase ();
var tmp2= $str. toLowerCase ();
Return TMP1==TMP2
}
}
Inserts the specified string after the specified position! Invalid index appended directly to the end of the string
String.prototype.insert = function ($ofset, $str) {
if ($ofset <0 | | | $ofset >=this.length-1) {return
This.concat ($STR);
Return this.substring (0, $ofset) + $str +this.substring ($ofset + 1);
}
Sets the character of the specified position to a different specified character or string. Invalid index will return directly without any processing!
String.prototype.setCharAt = function ($ofset, $str) {
if ($ofset <0 | | | $ofset >=this.length-1) {return
This.valueof ();
}
Return this.substring (0, $ofset) + $str +this.substring ($ofset + 1);
}
String.prototype.replaceLen = function (start, Len, replaced) {
if (!len) return this
;
if (start >= this.length) return this
;
var returnseg = ';
var returnSeg2 = ';
var i = 0;
for (; i < this.length; i++) {
var c = This.charat (i);
if (I < start)
returnseg + = C;
if (i >= start + len)
returnSeg2 + = c;
}
return returnseg + replaced + returnSeg2;
}
To extend the base class:
Replace the character, this is useful in the replacement, such as * * * * * * * * * * * * hour replacement for <input/> days <input/> Hours
String.prototype.replaceChar = function (target, replaced, start) {
if (!target) return this
;
if (!start)
start = 0;
var returnval = this.substring (0, start);
var index = 0;
for (var i = start; i < this.length; i++) {
var c = This.charat (i);
target = typeof target = = ' function '? Target.call (this, index): target;
if (c = = target) {
returnval + = typeof replaced = = ' function '? Replaced.call (this, index): replaced;
while (I < this.length-1 && This.charat (i + 1) = = c) {
i++;
}
index++;
} else{
ReturnVal + = c;
}
}
return returnval;
}
Sort the string in reverse order
String.prototype.reverse = function () {
var str= "";
for (Var i=this.length-1;i>=0;i--) {
str=str.concat (This.charat (i));
}
return str;
}
Calculates the length, each Chinese character occupies two length, the English character each occupies one length
String.prototype.ucLength = function () {
var len = 0;
for (Var i=0;i<this.length;i++) {
if (this.charcodeat (i) >255) len+=2;
else len++;
}
return len;
}
Fill some specific characters to the left of the string
String.prototype.lpad = function (len, s) {
var a = new Array (this);
var n = (len-this.length);
for (var i = 0; i < n; i++) {
a.unshift (s);
}
Return A.join ("");
}
Fill some specific characters to the right of the string
String.prototype.rpad = function (len, s) {
var a = new Array (this);
var n = (len-this.length);
for (var i = 0; i < n; i++) {
A.push (s);
}
Return A.join ("");
}
Converts the first letter of a string to uppercase
String.prototype.ucwords = function () {return
this.substring (0,1). toUpperCase (). Concat (this.substring (1));
String.prototype.contains = function ($str) {return
this.indexof ($STR) >-1? true:false;
}
Converts a string formatted as 2008-04-02 10:08:44 to a date (the value of a string object must be: 2008-04-02 10:08:44)
String.prototype.toDate = function () {
var str = this.replace (/-/g, "/");
Return (new Date (str));
Converts the decimal number that was originally represented by a string to a floating-point scale: precision to precision
String.prototype.toFloat = function (precision) {
precision = Precision | | 2;
return parsefloat (this,10). toFixed (precision);
Converts a decimal integer that was originally represented by a string
String.prototype.toInt = function () {return
parseint (this,10). toString ();
Adds two original decimal numbers expressed as strings and returns as strings: Addend is Addends
String.prototype.add = function (addend) {
var sum = parsefloat (this,10) + parsefloat (addend,10);
return sum+ "";
}
Decimal to turn other code into the following NeXtScale for the system as 2,8,16
String.prototype.shiftScale = function (NeXtScale) {return
parsefloat (this). ToString (NeXtScale);
Each system converts to each other:
This object must be an integer
@param Prescale is a few numbers
@param nextscale to be converted into several numbers
String.prototype.scaleShift = function (Prescale,nextscale) {return
parseint (This,prescale). ToString ( NeXtScale);
}
Full-angle 2 half-width document.write ("ABC 123, we are all good friends");
STRING.PROTOTYPE.DBC2SBC = function () {
Return This.replace (/[\uff01-\uff5e]/g,function (a) {return String.fromCharCode (a.charcodeat (0)-65248);}). Replace (/\u3000/g, "");
}
Array extension function:
var isnumeric = function (x) {//returns TRUE if X is numeric and false if it isn't. var RegExp =/^ (-)? (\d*) (\.?)
(\d*) $/;
return String (x). Match (REGEXP);
} var myarray = [1, ' Two ', 3, ' Four ', 5, ' Six ', 7, ' eight ', 9, ' Ten ']; var oddarray=myarray.filter (IsNumeric); outputs:1,3,5,7,9 var oddarray=myarray.some (isnumeric); outputs:true var oddarray=myarray.every (isnumeric);
Outputs:false var printArray =function (x, idx) {Document.writeln (' [' +idx+ '] = ' +x ');
} myarray.foreach (PrintArray);//outputs: [0] = 1 [1] = two [2] = 3 [3] = four [4] = 5 Myarray.remove (9);
Document.writeln (myarray); if (!
Array.prototype.every) {Array.prototype.every = function (Fun/*, thisp*/) {var len = this.length;
if (typeof fun!= "function") throw new TypeError ();
var thisp = arguments[1]; for (var i = 0; i < len; i++) {if (i) &&!fun.call (Thisp, this[i), I, this) return F
Alse;
return true;
}; } if (! ArraY.prototype.filter) {Array.prototype.filter = function (Fun/*, thisp*/) {var len = this.length;
if (typeof fun!= "function") throw new TypeError ();
var res = new Array ();
var thisp = arguments[1]; for (var i = 0; i < len; i++) {if (I, this) {var val = this[i];//into case fun mutates this if
(Fun.call (Thisp, Val, I, this)) Res.push (Val);
} return res;
}; } if (!
Array.prototype.forEach) {Array.prototype.forEach = function (Fun/*, thisp*/) {var len = this.length;
if (typeof fun!= "function") throw new TypeError ();
var thisp = arguments[1];
for (var i = 0; i < len; i++) {if (i) Fun.call (Thisp, this[i], I, this);
}
}; } if (!
Array.prototype.map) {Array.prototype.map = function (Fun/*, thisp*/) {var len = this.length;
if (typeof fun!= "function") throw new TypeError ();
var res = new Array (len);
var thisp = arguments[1]; for (VAR i = 0; i < Len;
i++) {if (i) res[i] = Fun.call (Thisp, this[i], I, this);
return res;
}; } if (!
Array.prototype.some) {Array.prototype.some = function (Fun/*, thisp*/) {var len = this.length;
if (typeof fun!= "function") throw new TypeError ();
var thisp = arguments[1]; for (var i = 0; i < len; i++) {if (i) && fun.call (Thisp, this[i), I, this) return TR
Ue
return false;
};
} Array.prototype.sortNum = function () {return this.sort (function (a,b) {return a-b;});
<!--var tmp = [5,9,12,18,56,1,10,42, ' Blue ', ' 7,97,53,33,30,35,27,30, ' ', ' Ball ', ' bubble ']; var thirty=tmp.find (30); Returns 9, var thirtyfive=tmp.find (' 35 '); Returns var thirtyfive=tmp.find (35); Returns var haveblue=tmp.find (' Blue '); Returns 8 var notfound=tmp.find (' not there! '); Returns false var regexp1=tmp.find (/^b/); Returns 8,20 (the Letter starts with B) var regexp1=tmp.find (/^b/i); Returns 8,19,20 (same as above but ignore case)--> Array.prototype.find = function (searchstr) {var returnarray
= false; For (i=0 i<this.length; i++) {if (typeof (searchstr) = = ' function ') {if (searchstr.test)) {if (!)
ReturnArray) {ReturnArray = []} returnarray.push (i);
} else {if (THIS[I]===SEARCHSTR) {if (!returnarray) {returnarray = []} returnarray.push (i);
}} return ReturnArray;
}
Ordering random changes in arrays
Array.prototype.shuffle = function () {for
(var rnd, TMP, i=this.length; i; Rnd=parseint (Math.random () *i), tmp=this[ ----------this[i]=this[rnd], this[rnd]=tmp);
return this;
}
<!--var myarray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
var yourarray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
Document.writeln (Myarray.compare (Yourarray)); outputs:true;-->
Array.prototype.compare = function (Testarr) {
if (this.length!= testarr.length) return false;
for (var i = 0; i < testarr.length i++) {
if (this[i].compare) {
if (!this[i].compare (testarr[i)) return FA LSE;
}
if (This[i]!== testarr[i]) return false;
return true;
}
Remove the duplicate value in the array var a = new Array ("5", "7", "7"); A.unique ();
Array.prototype.unique = function () {var data = this | |
[]; var a = {}; Declares an object that can be used as a hash table with a for (var i = 0; i < data.length; i++) {A[data[i]] = true;//set tag, the value of the array is now marked so that you can go
Drop duplicate Value} data.length = 0;
for (var i in a) {//Traversal object, put the labeled restore this[data.length] = i;
} return data;
} Array.prototype.addAll = function ($array) {if ($array = = NULL | | $array. length = 0) return;
for (var $i =0; $i < $array. length; $i + +) This.push ($array [$i]); } Array.prototype.contains = function ($value) {for (var $i =0; $i <this.length; $i + +) {var $element = thi
s[$i];
if ($element = = $value) return true;
return false; } Array.prototype.indexOf = function ($value) {for (var $i =0; $i <this.length; $i + +) {if (this[$i] = = $val
UE) return $i;
} return-1; } if (! ARRAY.PROTOTYPE.LASTINDEXOF) {Array.prototype.lastIndexOf = function (ELT/*, from*/) {var len = THIS.LEngth;
var from = number (arguments[1]);
if (isNaN) {from = len-1; else {from = (from < 0)?
Math.ceil (from): Math.floor (from);
if (from < 0) from + = Len;
else if (from >= len) from = len-1;
for (; from >-1; from--) {if this[from] = = ELT) return from;
} return-1;
};
} Array.prototype.insertAt = function ($value, $index) {if ($index < 0) this.unshift ($value);
else if ($index >= this.length) This.push ($value);
else This.splice ($index, 0, $value);
}
To delete an element based on the subscript of an array
Array.prototype.removebyindex=function ($n) {if
($n <0) {//If n<0, no action is made. return this
;
} else{return
this.slice (0, $n). Concat (This.slice ($n +1,this.length));
}
Reliance on IndexOf
Array.prototype.remove = function ($value) {var $index = This.indexof ($value);
if ($index!=-1) this.splice ($index, 1);
} Array.prototype.removeAll = function () {while (This.length > 0) this.pop (); } Array.prototype.replace = function ($oldValue, $newValue) {for (var $i =0; $i <this.length; $i + +) {if (th
is[$i] = = $oldValue) {this[$i] = $newValue;
Return
}} Array.prototype.swap = function ($a, $b) {if ($a = = $b) return;
var $tmp = this[$a];
this[$a] = this[$b];
this[$b] = $tmp;
} Array.prototype.max = function () {return Math.max.apply ({}, this);
} Array.prototype.min = function () {return Math.min.apply ({}, this);
} Array.prototype.splice = function (Start, Dellen, item) {var len =this.length;
Start = start<0?0:start>len?len:start?start:0;
dellen=dellen<0?0:dellen>len?len:dellen?dellen:len;
var arr =[],res=[];
var iarr=0,ires=0,i=0;
for (i=0;i<len;i++) {if (i<start| | ires>=dellen) arr[iarr++]=this[i];
else {res[ires++]=this[i];
if (Item&&ires==dellen) {arr[iarr++]=item;
}} if (Item&&ires<dellen) Arr[iarr]=item;
for (Var i=0;i<arr.length;i++) {This[i]=arr[i];
} this.length=arr.length;
return res;
} Array.prototype.shift = function () {if (!this) Return[];return this.splice (0,1) [0];}
Add separately, keyword shallow copy, if you encounter an array, copy the elements in the array
Array.prototype.concat = function () {var i=0; while (i<arguments.length) {if (typeof arguments[i] = = ' object ' &&typeof arguments[i].splice = = ' function ' & Amp;&!arguments[i].propertyisenumerable (' length ')) {//not shallow COPY BELOW//ARRAY.PROTOTYPE.CONCAT.APPL
Y (this,arguments[i++]);
var j=0;
while (j<arguments[i].length) This.splice (this.length,0,arguments[i][j++]);
i++;
} else{this[this.length]=arguments[i++];
} return this;
} Array.prototype.join = function (separator) {var i=0,str= "";
while (i<this.length) Str+=this[i++]+separator;
return str;
} Array.prototype.pop = function () {return this.splice (this.length-1,1) [0];} Array.prototype.push = function () {Array.prototype.splice.apply (this, [This.length,0].concat Array.prototype.slic E.apply (arguments)));
There is no direct processing parameters, but a copy of the return this.length; } Array.prototype.reverse = function () {for (Var i=0;i<this.length/2;i++) {var temp = this[i];
This[i]= This[this.length-1-i];
This[this.length-1-i] = temp;
return to this;
} Array.prototype.slice = function (start, end) {var len =this.length;
start=start<0?start+=len:start?start:0;
End =end<0?end+=len:end>len?len:end?end:len;
var I=start;
var res = [];
while (i<end) {Res.push (this[i++]);
return res;
}//arr.unshift (Ele1,ele2,ele3 ...) Array.prototype.unshift =function () {Array.prototype.splice.apply (This,[0,0].concat Array.prototype.slice.apply (
this,arguments))); }