String对象的扩展函数:
}
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);
}
变量替换
在字符串末尾追加字符串
删除指定索引间的字符串.$sIndex和$eIndex所在的字符不被删除!依赖deleteCharAt
检查字符串是否以某个字符串(str)结尾
检查该字符串是否以某个字符串开始
比较两个字符串是否相等,不区分大小写!
将指定的字符串插入到指定的位置后面!索引无效将直接追加到字符串的末尾
将指定的位置的字符设置为另外指定的字符或字符串.索引无效将直接返回不做任何处理!
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;
}
扩展基础类: 替换字符,这个在替换填入比较有用,比如***天***小时 替换为 天小时
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;
}
将该字符串反序排列
计算长度,每个汉字占两个长度,英文字符每个占一个长度
在字符串的左边填充一些特定的字符
在字符串的右边填充一些特定的字符
把字符串的首字母转化为大写
将格式为2008-04-02 10:08:44的字符串转成日期(String对象的值必须为: 2008-04-02 10:08:44)
将原来用字符串表示的十进数转成十进制浮点数: precision为精度
将原来用字符串表示的十进数转成十进制整数
将两个原来用字符串表示的十进数相加后当作字串返回 : addend为加数
十进制转其他进制代码如下nextScale为进制 如2,8,16
各进制互相转换 : this对象必须是整数 @param preScale 原是是几进制数 @param nextScale 要转换成几进制数
全角2半角 document.write("ABC 123,我们都是好朋友"); String.prototype.dbc2sbc = function (){ return this.replace(/[\uff01-\uff5e]/g,function(a){return String.fromCharCode(a.charCodeAt(0)-65248);}).replace(/\u3000/g," "); }
Array扩展函数:
{
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 in this &&
!fun.call(thisp,this[i],this))
return false;
}
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 in this)
{
var val = this[i]; // in case fun mutates this
if (fun.call(thisp,val,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 in this)
fun.call(thisp,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 in this)
res[i] = fun.call(thisp,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 in this &&
fun.call(thisp,this))
return true;
}
return false;
};
}
Array.prototype.sortNum = function() {
return this.sort( function (a,b) { return a-b; } );
}
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;
}
随机改变数组的排序
去掉数组中重复的值var a = new Array("5","7","7"); a.unique();
for (var i in a) { //遍历对象,把已标记的还原成数组
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 = this[$i];
if($element == $value)
return true;
}
return false;
}
Array.prototype.indexOf = function($value)
{
for(var $i=0; $i<this.length; $i++)
{
if(this[$i] == $value)
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))
{
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 (from in this &&
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,$value);
}
根据数组的下标来删除元素
依赖indexOf
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;
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];}
分开添加,关键字shallow copy,如果遇到数组,复制数组中的元素
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.slice.apply(arguments))); //这里没有直接处理参数,而是复制了一下
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 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)));
}