欢迎光临
我们一直在努力

javascript 函数原型-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

string .length 汉字为两个字符处理

.tlength

——————————————————————————–

<script>

/*

this following code are designed and writen by windy_sk <seasonx@163.net>

you can use it freely, but u must held all the copyright items!

*/

//测字符串实际长度

string.prototype.tlength = function(){var arr=this.match(/[^\x00-\xff]/ig);return this.length+(arr==null?0:arr.length);}

//字符串左取

string.prototype.left = function(num,mode){if(!/\d+/.test(num))return(this);var str = this.substr(0,num);if(!mode) return str;var n = str.tlength() – str.length;num = num – parseint(n/2);return this.substr(0,num);}

//字符串右取

string.prototype.right = function(num,mode){if(!/\d+/.test(num))return(this);var str = this.substr(this.length-num);if(!mode) return str;var n = str.tlength() – str.length;num = num – parseint(n/2);return this.substr(this.length-num);}

//字符串包含

string.prototype.getcount = function(str,mode){return eval("this.match(/("+str+")/g"+(mode?"i":"")+").length");}

//字符串去除两端空字符

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, "");}

document.write("aa啦啦aa.tlength() = " + "aa啦啦aa".tlength());

document.write("<br>");

document.write("aabbcc.tlength() = " + "aabbcc".tlength());

document.write("<br>");

document.write("aa啦啦aa.left(4) = " + "aa啦啦aa".left(4));

document.write("<br>");

document.write("aa啦啦aa.left(4,true) = " + "aa啦啦aa".left(4,true));

document.write("<br>");

document.write("aa啦啦aa.right(4) = " + "aa啦啦aa".right(4));

document.write("<br>");

document.write("aa啦啦aa.right(4,true) = " + "aa啦啦aa".right(4,true));

document.write("<br>");

document.write("abacae.getcount(a) = " + "abacae".getcount("a"));

document.write("<br>");

document.write("abacae.getcount(a,true) = " + "abacae".getcount("a",true));

document.write("<br>");

document.write(" abc de .ltrim() = " + " abc de ".ltrim());

document.write("<br>");

document.write(" abc de .rtrim() = " + " abc de ".rtrim());

document.write("<br>");

document.write(" abc de .trim() = " + " abc de ".trim());

</script>

再来几个函数,getcookie(),getquery(),datediff(),dateadd(),b2s(),原+改

——————————————————————————–

function getcookie(mainkey,subkey) {

var reg = new regexp("(^| )"+mainkey+"=([^;]*)(;|$)");

var arr = document.cookie.match(reg);

if (arguments.length == 2) {

if (arr!=null)

return key(subkey,arr[2]);

else

return null;

} else if (arguments.length == 1) {

if (arr!=null)

return unescape(arr[2]);

else

return null;

}

function key(subkey,findwith) {

var arr,reg = new regexp("(^| |&)"+subkey+"=([^&]*)(&|$)");

var findwith = findwith?findwith:document.cookie;

if (arr = findwith.match(reg))

return unescape(arr[2]);

else

return null;

}

}

function getquery(name) {

var reg = new regexp("(^|&)"+name+"=([^&]*)(&|$)");

var r = location.search.substr(1).match(reg)

if (r!=null) return unescape(r[2]);

}

function dateadd(strinterval, numday, dtdate) {

var dttmp = new date(dtdate);

if (isnan(dttmp)) dttmp = new date();

switch (strinterval) {

case "s":return new date(date.parse(dttmp) + (1000 * numday));

case "n":return new date(date.parse(dttmp) + (60000 * numday));

case "h":return new date(date.parse(dttmp) + (3600000 * numday));

case "d":return new date(date.parse(dttmp) + (86400000 * numday));

case "w":return new date(date.parse(dttmp) + ((86400000 * 7) * numday));

case "m":return new date(dttmp.getfullyear(), (dttmp.getmonth()) + numday, dttmp.getdate(), dttmp.gethours(), dttmp.getminutes(), dttmp.getseconds());

case "y":return new date((dttmp.getfullyear() + numday), dttmp.getmonth(), dttmp.getdate(), dttmp.gethours(), dttmp.getminutes(), dttmp.getseconds());

}

}

// datediff("m","2002/12/1 00:00:00","2004/12/1 00:00:00")

function datediff(strinterval, dtstart, dtend) {

var dtstart = new date(dtstart);

if (isnan(dtstart)) dtstart = new date();

var dtend = new date(dtend);

if (isnan(dtend)) dtend = new date();

switch (strinterval) {

case "s":return parseint((dtend – dtstart) / 1000);

case "n":return parseint((dtend – dtstart) / 60000);

case "h":return parseint((dtend – dtstart) / 3600000);

case "d":return parseint((dtend – dtstart) / 86400000);

case "w":return parseint((dtend – dtstart) / (86400000 * 7));

case "m":return (dtend.getmonth()+1)+((dtend.getfullyear()-dtstart.getfullyear())*12) – (dtstart.getmonth()+1);

case "y":return dtend.getfullyear() – dtstart.getfullyear();

}

}

// b2s("11新浪网!@#程序员_+}{:><")

function b2s(strng) {

var matches = strng.match(/[\x21-\x7e]/g);

for (var i=0;i<matches.length;i++) {

strng = strng.replace(matches[i],string.fromcharcode(matches[i].charcodeat (0)+65248))

}

return strng;

}

来几个,ie5下原型扩展

——————————————————————————–

/*number object*/

if(!number.prototype.tofixed) {

number.prototype.tofixed = function(n) {

with(math) return round(number(this)*pow(10,n))/pow(10,n)

}

}

/*array object*/

if (!array.prototype.pop) {

array.prototype.pop = function() {

var lastelement = this[this.length-1];

this.length = math.max(this.length-1,0);

return lastelement;

}

}

if (!array.prototype.push) {

array.prototype.push = function() {

for(var i=0;i<arguments.length;i++) {

this[this.length]=arguments[i];

}

return this.length;

}

}

if (!array.prototype.shift) {

array.prototype.shift = function() {

var firstelement = this[0];

this.reverse();

this.pop();

this.reverse();

return firstelement;

}

}

if (!array.prototype.splice) {

array.prototype.splice = function() {

var start = arguments[0];

var deletecount = start+arguments[1];

var deleteitem = this.slice(start,deletecount);

var beforeitem = this.slice(0,start);

var afteritem = this.slice(deletecount);

this.length=beforeitem.length;

var i;

for (i=2;i<arguments.length;this[this.length]=arguments[i++]);

for (i=0;i<afteritem.length;this[this.length]=afteritem[i++]);

return deleteitem;

}

}

if (!array.prototype.unshift) {

array.prototype.unshift = function() {

var arr = new array();

for (var i=0;i<arguments.length;arr[i]=arguments[i++]);

arr = arr.concat(this);

this.length = 0;

for (i=0;i<arr.length;this[i]=arr[i++]);

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » javascript 函数原型-JSP教程,Java技巧及代码
分享到: 更多 (0)