String 对象的扩展函数 :
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 = fonction ($ Delimiter, $ limit) {var $ ss = this.split ($ Delimiter, $ limit); pour (var $ i = 0; $ i <$ ss.length; $ i ++) $ ss [$ i] = $ ss [$ i] .trim (); retourner $ ss; } String.prototype.htmlentities = function () {return this.replace (/ & / g, '&'). Remplace (/ </ g, '<'). Remplace (/> / g, '>'); } String.prototype.striptiptags = 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 = fonction ($ old, $ snew) {return this.replace (new regexp ($ old, "gm"), $ snew); }变量替换
var a = "j'aime {0}, et vous aimez {1}, où sont {0}!"; a.format ("vous", "moi"); String.prototype.format = function () {var args = arguments; return this.replace (// {(/ d +) /} / g, fonction (m, i, o, n) {return args [i];}); }在字符串末尾追加字符串
String.prototype.append = function ($ str) {return this.concat ($ str); }删除指定索引位置的字符 , 索引无效将不删除任何字符
String.prototype.deletecharat = fonction ($ 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); }}删除指定索引间的字符串. $ Sindex 和 $ eindex 所在的字符不被删除!依赖 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; Renvoie this.substring (0, $ Sindex + 1) + this.Substring ($ eindex, this.length); }}检查字符串是否以某个字符串 (str) 结尾
String.prototype.endswith = function ($ str) {return this.substr (this.length - $ str.length) == $ str; }检查该字符串是否以某个字符串开始
String.prototype.startswith = function (str) {return this.substr (0, str.length) == str; }比较两个字符串是否相等 , 不区分大小写!
String.prototype.equalsignorecase = function ($ str) {if (this.length! = $ Str.length) {return false; } else {var tmp1 = this.tolowercase (); var tmp2 = $ str.tolowercase (); retour tmp1 == tmp2; }}将指定的字符串插入到指定的位置后面! 索引无效将直接追加到字符串的末尾
String.prototype.insert = function ($ ofseset, $ str) {if ($ of -set <0 || $ ofset> = this.length-1) {return this.concat ($ str); } return this.substring (0, $ ofseset) + $ str + this.substring ($ ofseset + 1); }将指定的位置的字符设置为另外指定的字符或字符串. 索引无效将直接返回不做任何处理!
String.prototype.Setcharat = function ($ ofseset, $ str) {if ($ ofseset <0 || $ ofseset> = this.length-1) {return this.valueof (); } return this.substring (0, $ ofseset) + $ str + this.substring ($ ofseset + 1); } String.prototype.replacelen = fonction (start, len, remplacé) {if (! Len) renvoie ceci; if (start> = this.length) renvoie ceci; 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 + remplacé + returnseg2; }:
替换字符 , 这个在替换填入比较有用 , 比如 *** 天 *** 小时 替换为 <entrée /> 天 <entrée /> 小时
String.prototype.replaceChar = fonction (cible, remplacé, start) {if (! Target) renvoie ceci; 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 == 'Fonction'? Target.Call (this, index): Target; if (c == cible) {returnVal + = typeof remplacé == 'fonction'? remplacé.Call (this, index): remplacé; while (i <this.length - 1 && this.charat (i + 1) == c) {i ++; } index ++; } else {returnval + = c; }} return returnVal; }将该字符串反序排列
String.prototype.reverse = function () {var str = ""; for (var i = this.length-1; i> = 0; i -) {str = str.concat (this.charat (i)); } return str; }计算长度 , 每个汉字占两个长度 , 英文字符每个占一个长度
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; }在字符串的左边填充一些特定的字符
String.prototype.lpad = fonction (len, s) {var a = new Array (this); var n = (len - this.length); pour (var i = 0; i <n; i ++) {a.unShift (s); } return a.join (""); }在字符串的右边填充一些特定的字符
String.prototype.rpad = fonction (len, s) {var a = new Array (this); var n = (len - this.length); pour (var i = 0; i <n; i ++) {a.push (s); } return a.join (""); }把字符串的首字母转化为大写
String.prototype.ucwords = function () {return this.substring (0,1) .touppercase (). Concat (this.substring (1)); } String.prototype.contains = fonction ($ str) {return this.indexof ($ str)> -1? vrai: false; }将格式为 2008-04-02 10:08:44 的字符串转成日期 (String 对象的值必须为: 2008-04-02 10:08:44)
String.prototype.todate = function () {var str = this.replace (/ - / g, "/"); retour (nouvelle date (str)); }将原来用字符串表示的十进数转成十进制浮点数: précision 为精度
String.prototype.tofloat = fonction (précision) {précision = précision || 2; Retour parsefloat (this, 10) .tofixed (précision); }将原来用字符串表示的十进数转成十进制整数
String.prototype.toint = function () {return parseInt (this, 10) .toString (); }将两个原来用字符串表示的十进数相加后当作字串返回: Addend 为加数
String.prototype.add = fonction (addend) {var sum = parsefloat (this, 10) + parsefloat (addend, 10); RETOUR SUM + ""; }十进制转其他进制代码如下 NextScale 为进制 如 2,8,16
String.prototype.shiftsCale = function (nextScale) {return parsefloat (this) .toString (nextScale); }:
ce 对象必须是整数
@param prescale 原是是几进制数
@param nextScale 要转换成几进制数
String.prototype.sCaleshift = function (prescale, nextScale) {return parseInt (this, prescale) .toString (nextScale); }全角 2 半角 document.write ("abc 123 , 我们都是好朋友");
String.prototype.dbc2sbc = fonction () {
return this.replace (/ [/ uff01- / uff5e] / g, fonction (a) {return string.fromCharcode (a.CharCodeat (0) -65248);}). Remplace (// u3000 / g, "");
}
Tableau : :
var isNumeric = function (x) {// renvoie true si x est numérique et faux si ce n'est pas le cas. var regexp = /^(-)?(/d*)(/.?)(/D*)$/ /; retour de la chaîne (x) .match (regexp); } var myArray = [1, «Two», 3, «Four», 5, «Six», 7, «Eight», 9, «Ten»]; var udArray = MyArray.filter (isNumeric); // Sorties: 1,3,5,7,9 var odaDarray = MyArray.Some (isNumeric); // Sorties: vrai var odArray = MyArray.every (isNumeric); // sorties: false var printRay = function (x, idx) {document.writeln ('[' + idx + '] =' + x); } myArray.ForEach (printArray); // Sorties: [0] = 1 [1] = deux [2] = 3 [3] = quatre [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") lancez new typeError (); var thisp = arguments [1]; pour (var i = 0; i <len; i ++) {if (i dans ce &&! fun.call (thisp, this [i], i, this)) return false; } return true; }; } if (! array.prototype.filter) {array.prototype.filter = function (fun / *, thisp * /) {var len = this.length; if (typeof fun! = "function") lancez new typeError (); var res = new Array (); var thisp = arguments [1]; for (var i = 0; i <len; i ++) {if (i in ce) {var val = this [i]; // au cas où le plaisir mute si (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") lancez new typeError (); var thisp = arguments [1]; for (var i = 0; i <len; i ++) {if (i in ce) fun.call (thisp, this [i], i, this); }}; } if (! array.prototype.map) {array.prototype.map = fonction (fun / *, thisp * /) {var len = this.length; if (typeof fun! = "function") lancez new typeError (); var res = nouveau tableau (len); var thisp = arguments [1]; pour (var i = 0; i <len; i ++) {if (i dans ce) 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") lancez new typeError (); var thisp = arguments [1]; pour (var i = 0; i <len; i ++) {if (i dans ce && fun.call (thisp, this [i], i, this)) return true; } return false; }; } Array.prototype.sortnum = function () {return this.sort (fonction (a, b) {return ab;}); } <! - var tmp = [5,9,12,18,56,1,10,42, 'bleu', 30, 7,97,53,33,30,35,27,30, '35 ',' ball ',' bubble ']; var trente = tmp.find (30); // retourne 9, 14, 17 var trentefive = tmp.find ('35 '); // Renvoie 18 var trentefive = tmp.find (35); // Renvoie 15 var haveblue = tmp.find ('bleu'); // Renvoie 8 var notfound = tmp.find ('pas là!'); // Renvoie false var regexp1 = tmp.find (/ ^ b /); // Renvoie 8,20 (la première lettre commence par b) var regexp1 = tmp.find (/ ^ b / i); // Renvoie 8,19,20 (comme ci-dessus mais ignorer le cas) -> array.prototype.find = function (searchstr) {var returnArray = false; for (i = 0; i <this.length; i ++) {if (typeof (searchstr) == 'function') {if (searchstr.test (this [i])) {if (! returnArray) {returnArray = []} returnArray.push (i); }} else {if (this [i] === searchstr) {if (! returnArray) {returnArray = []} returnArray.push (i); }}} return returnArray; }随机改变数组的排序
Array.prototype.shuffle = function () {for (var rnd, tmp, i = this.length; i; rnd = paSeInt (math.random () * i), tmp = this [- i], this [i] = this [rnd], this [rnd] = tmp); retourner ceci; } <! - 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)); // sorties: 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 false; } if (this [i]! == testarr [i]) return false; } return true; }去掉数组中重复的值 var a = nouveau tableau ("5", "7", "7"); A.Unique ();
Array.prototype.Unique = function () {var data = this || []; var a = {}; // 声明一个对象 , javascript 的对象可以当哈希表用 pour (var i = 0; i <data.length; i ++) {a [data [i]] = true; // 设置标记 , 把数组的值当下标 , 这样就可以去掉重复的值} data.length = 0; pour (var i dans a) {// 遍历对象 , 把已标记的还原成数组 this [data.length] = i; } return data; } Array.prototype.addall = fonction ($ 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 = this [$ i]; if ($ element == $ valeur) renvoie true; } return false; } Array.prototype.indexof = fonction ($ value) {for (var $ i = 0; $ i <this.length; $ i ++) {if (this [$ i] == $ value) return $ i; } return -1; } if (! array.prototype.lastIndexof) {array.prototype.lastIndexof = fonction (elt / *, from * /) {var len = this.length; var de = nombre (arguments [1]); if (isnan (from)) {from = len - 1; } else {from = (de <0)? Math.ceil (de): math.floor (de); if (de <0) de + = len; else if (de> = len) de = len - 1; } pour (; de> -1; de--) {if (de dans ce && this [from] === elt) return de; } return -1; }; } Array.prototype.insertat = fonction ($ value, $ index) {if ($ index <0) this.unshift ($ value); else if ($ index> = this.length) this.push ($ value); else this.splice ($ index, 0, $ value); }根据数组的下标来删除元素
Array.prototype.reMoveByIndex = fonction ($ n) {if ($ n <0) {// 如果 n <0 , 则不进行任何操作。 renvoie ceci; } else {return this.slice (0, $ n) .concat (this.slice ($ n + 1, this.length)); }}依赖 Index de
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 (this [$ i] == $ oldvalue) {this [$ i] = $ newValue; retour; }}} Array.prototype.swap = fonction ($ a, $ b) {if ($ a == $ b) return; var $ tmp = this [$ a]; ce [$ a] = ce [$ b]; ce [$ b] = $ tmp; } Array.prototype.max = function () {return math.max.apply ({}, this); } Array.prototype.min = fonction () {return math.min.apply ({}, this); } Array.prototype.splice = fonction (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; pour (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];}分开添加, 关键字 Copie peu profonde, 如果遇到数组 , 复制数组中的元素
Array.prototype.concat = function () {var i = 0; while (i <arguments.length) {if (typeof arguments [i] === 'object' && typeof arguments [i] .splice === 'function' &&! arguments [i] .propertyisenuableable ('longueur')) {// non peu profond copie ci-dessous // array.prototype.concat.apply (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 ++]; }} renvoie ceci; } Array.prototype.join = fonction (séparateur) {var i = 0, str = ""; while (i <this.length) str + = this [i ++] + séparateur; retour 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.slice.apply (arguments)); // 这里没有直接处理参数 , 而是复制了一下 renvoie ce.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; } retourne ceci; } Array.prototype.slice = fonction (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)))); }