1 /** 在字符串末尾追加字符串 **/
2 String.prototype.append = function (str) {
3 return this.concat(str);
4 }
1 /** 删除指定索引位置的字符,索引无效将不删除任何字符 **/
2 String.prototype.deleteCharAt = function (index) {
3 if (index < 0 || index >= this.length) {
4 return this.valueOf();
5 }
6 else if (index == 0) {
7 return this.substring(1, this.length);
8 }
9 else if (index == this.length - 1) {
10 return this.substring(0, this.length - 1);
11 }
12 else {
13 return this.substring(0, index) + this.substring(index + 1);
14 }
15 }
1 /** 删除指定索引区间的字符串 **/
2 String.prototype.deleteString = function (start, end) {
3 if (start == end) {
4 return this.deleteCharAt(start);
5 }
6 else {
7 if (start > end) {
8 var temp = start;
9 start = end;
10 end = temp;
11 }
12 if (start < 0) {
13 start = 0;
14 }
15 if (end > this.length - 1) {
16 end = this.length - 1;
17 }
18 return this.substring(0, start) + this.substring(end +1 , this.length);
19 }
20 }
1 /** 检查字符串是否以subStr结尾 **/
2 String.prototype.endWith = function (subStr) {
3 if (subStr.length > this.length) {
4 return false;
5 }
6 else {
7 return (this.lastIndexOf(subStr) == (this.length - subStr.length)) ? true : false;
8 }
9 }
1 /** 比较两个字符串是否相等,也可以直接用 == 进行比较 **/
2 String.prototype.equal = function (str) {
3 if (this.length != str.length) {
4 return false;
5 }
6 else {
7 for (var i = 0; i < this.length; i++) {
8 if (this.charAt(i) != str.charAt(i)) {
9 return false;
10 }
11 }
12 return true;
13 }
14 }
1 /** 比较两个字符串是否相等,不区分大小写 **/
2 String.prototype.equalIgnoreCase = function (str) {
3 var temp1 = this.toLowerCase();
4 var temp2 = str.toLowerCase();
5 return temp1.equal(temp2);
6 }
1 /** 将指定的字符串插入到指定的位置后面,索引无效将直接追加到字符串的末尾 **/
2 String.prototype.insert = function (ofset, subStr) {
3 if (ofset < 0 || ofset >= this.length - 1) {
4 return this.append(subStr);
5 }
6 return this.substring(0, ofset + 1) + subStr + this.substring(ofset + 1);
7 }
1 /** 判断字符串是否数字串 **/
2 String.prototype.isAllNumber = function () {
3 for (var i = 0; i < this.length; i++) {
4 if (this.charAt(i) < '0' || this.charAt(i) > '9') {
5 return false;
6 }
7 }
8 return true;
9 }
1 /** 将字符串反序排列 **/
2 String.prototype.reserve = function () {
3 var temp = "";
4 for (var i = this.length - 1; i >= 0; i--) {
5 temp = temp.concat(this.charAt(i));
6 }
7 return temp;
8 }
1 /** 将指定的位置的字符设置为另外指定的字符或字符串.索引无效将直接返回不做任何处理 **/
2 String.prototype.setCharAt = function (index, subStr) {
3 if (index < 0 || index > this.length - 1) {
4 return this.valueOf();
5 }
6 return this.substring(0, index) + subStr + this.substring(index+1);
7 }
1 /** 检查字符串是否以subStr开头 **/
2 String.prototype.startWith = function (subStr) {
3 if (subStr.length > this.length) {
4 return false;
5 }
6 return (this.indexOf(subStr) == 0) ? true : false;
7 }
1 /** 计算长度,每个汉字占两个长度,英文字符每个占一个长度 **/
2 String.prototype.charLength = function () {
3 var temp = 0;
4 for (var i = 0; i < this.length; i++) {
5 if (this.charCodeAt(i) > 255) {
6 temp += 2;
7 }
8 else {
9 temp += 1;
10 }
11 }
12 return temp;
13 }
14 String.prototype.charLengthReg = function () {
15 return this.replace(/[^\x00-\xff]/g, "**").length;
16 }
1 /** 去掉首尾空格 **/
2 String.prototype.trim = function () {
3 return this.replace(/(^\s*)|(\s*$)/g, "");
4 }
1 /** 测试是否是数字 **/
2 String.prototype.isNumeric = function () {
3 var tmpFloat = parseFloat(this);
4 if (isNaN(tmpFloat))
5 return false;
6 var tmpLen = this.length - tmpFloat.toString().length;
7 return tmpFloat + "0".Repeat(tmpLen) == this;
8 }
1 /** 测试是否是整数 **/
2 String.prototype.isInt = function () {
3 if (this == "NaN")
4 return false;
5 return this == parseInt(this).toString();
6 }
1 /** 获取N个相同的字符串 **/
2 String.prototype.Repeat = function (num) {
3 var tmpArr = [];
4 for (var i = 0; i < num; i++) tmpArr.push(this);
5 return tmpArr.join("");
6 }
1 /** 合并多个空白为一个空白 **/
2 String.prototype.resetBlank = function () {
3 return this.replace(/s+/g, " ");
4 }
1 /** 除去左边空白 **/
2 String.prototype.LTrim = function () {
3 return this.replace(/^s+/g, "");
4 }
1 /** 除去右边空白 **/
2 String.prototype.RTrim = function () {
3 return this.replace(/s+$/g, "");
4 }
1 /** 除去两边空白 **/
2 String.prototype.trim = function () {
3 return this.replace(/(^s+)|(s+$)/g, "");
4 }
1 /** 保留数字 **/
2 String.prototype.getNum = function () {
3 return this.replace(/[^d]/g, "");
4 }
1 /** 保留字母 **/
2 String.prototype.getEn = function () {
3 return this.replace(/[^A-Za-z]/g, "");
4 }
1 /** 保留中文 **/
2 String.prototype.getCn = function () {
3 return this.replace(/[^u4e00-u9fa5uf900-ufa2d]/g, "");
4 }
1 /** 得到字节长度 **/
2 String.prototype.getRealLength = function () {
3 return this.replace(/[^x00-xff]/g, "--").length;
4 }
1 /** 从左截取指定长度的字串 **/
2 String.prototype.left = function (n) {
3 return this.slice(0, n);
4 }
1 /** 从右截取指定长度的字串 **/
2 String.prototype.right = function (n) {
3 return this.slice(this.length - n);
4 }
1 /*** 删除首尾空格 ***/
2 String.prototype.Trim = function() {
3 return this.replace(/(^\s*)|(\s*$)/g, "");
4 }
1 /*** 统计指定字符出现的次数 ***/
2 String.prototype.Occurs = function(ch) {
3 // var re = eval("/[^"+ch+"]/g");
4 // return this.replace(re, "").length;
5 return this.split(ch).length-1;
6 }
1 /*** 检查是否由数字组成 ***/
2 String.prototype.isDigit = function() {
3 var s = this.Trim();
4 return (s.replace(/\d/g, "").length == 0);
5 }
1 /*** 检查是否由数字字母和下划线组成 ***/
2 String.prototype.isAlpha = function() {
3 return (this.replace(/\w/g, "").length == 0);
4 }
1 /*** 检查是否为数 ***/
2 String.prototype.isNumber = function() {
3 var s = this.Trim();
4 return (s.search(/^[+-]?[0-9.]*$/) >= 0);
5 }
1 /*** 返回字节数 ***/
2 String.prototype.lenb = function() {
3 return this.replace(/[^\x00-\xff]/g,"**").length;
4 }
1 /*** 检查是否包含汉字 ***/
2 String.prototype.isInChinese = function() {
3 return (this.length != this.replace(/[^\x00-\xff]/g,"**").length);
4 }
1 /*** 简单的email检查 ***/
2 String.prototype.isEmail = function() {
3 var strr;
4 var mail = this;
5 var re = /(\w+@\w+\.\w+)(\.{0,1}\w*)(\.{0,1}\w*)/i;
6 re.exec(mail);
7 if(RegExp.$3!="" && RegExp.$3!="." && RegExp.$2!=".")
8 strr = RegExp.$1+RegExp.$2+RegExp.$3;
9 else
10 if(RegExp.$2!="" && RegExp.$2!=".")
11 strr = RegExp.$1+RegExp.$2;
12 else
13 strr = RegExp.$1;
14 return (strr==mail);
15 }
1 /*** 简单的日期检查,成功返回日期对象 ***/
2 String.prototype.isDate = function() {
3 var p;
4 var re1 = /(\d{4})[年./-](\d{1,2})[月./-](\d{1,2})[日]?$/;
5 var re2 = /(\d{1,2})[月./-](\d{1,2})[日./-](\d{2})[年]?$/;
6 var re3 = /(\d{1,2})[月./-](\d{1,2})[日./-](\d{4})[年]?$/;
7 if(re1.test(this)) {
8 p = re1.exec(this);
9 return new Date(p[1],p[2],p[3]);
10 }
11 if(re2.test(this)) {
12 p = re2.exec(this);
13 return new Date(p[3],p[1],p[2]);
14 }
15 if(re3.test(this)) {
16 p = re3.exec(this);
17 return new Date(p[3],p[1],p[2]);
18 }
19 return false;
20 }
1 /*** 检查是否有列表中的字符字符 ***/
2 String.prototype.isInList = function(list) {
3 var re = eval("/["+list+"]/");
4 return re.test(this);
5 }
1 // 清除两边的空格
2 String.prototype.trim = function() {
3 return this.replace(/(^\s*)|(\s*$)/g, '');
4 };
1 // 合并多个空白为一个空白
2 String.prototype.ResetBlank = function() {
3 var regEx = /\s+/g;
4 return this.replace(regEx, ' ');
5 };
1 String.prototype.GetNum = function() {
2 var regEx = /[^\d]/g;
3 return this.replace(regEx, '');
4 };
1 // 保留中文
2 String.prototype.GetCN = function() {
3 var regEx = /[^\u4e00-\u9fa5\uf900-\ufa2d]/g;
4 return this.replace(regEx, '');
5 };
1 // String转化为Number
2 String.prototype.ToInt = function() {
3 return isNaN(parseInt(this)) ? this.toString() : parseInt(this);
4 };
1 // 得到字节长度
2 String.prototype.GetLen = function() {
3 var regEx = /^[\u4e00-\u9fa5\uf900-\ufa2d]+$/;
4 if (regEx.test(this)) {
5 return this.length * 2;
6 } else {
7 var oMatches = this.match(/[\x00-\xff]/g);
8 var oLength = this.length * 2 - oMatches.length;
9 return oLength;
10 }
11 };
1 // 获取文件全名
2 String.prototype.GetFileName = function() {
3 var regEx = /^.*\/([^\/\?]*).*$/;
4 return this.replace(regEx, '$1');
5 };
1 // 获取文件扩展名
2 String.prototype.GetExtensionName = function() {
3 var regEx = /^.*\/[^\/]*(\.[^\.\?]*).*$/;
4 return this.replace(regEx, '$1');
5 };
1 String.Format = function() {
2 if (arguments.length == 0) {
3 return '';
4 }
5
6 if (arguments.length == 1) {
7 return arguments[0];
8 }
9
10 var reg = /{(\d+)?}/g;
11 var args = arguments;
12 var result = arguments[0].replace(reg, function($0, $1) {
13 return args[parseInt($1) + 1];
14 });
15 return result;
16 };
1 // 数字补零
2 Number.prototype.LenWithZero = function(oCount) {
3 var strText = this.toString();
4 while (strText.length < oCount) {
5 strText = '0' + strText;
6 }
7 return strText;
8 };
1 // Unicode还原
2 Number.prototype.ChrW = function() {
3 return String.fromCharCode(this);
4 };
1 // 数字数组由小到大排序
2 Array.prototype.Min2Max = function() {
3 var oValue;
4 for (var i = 0; i < this.length; i++) {
5 for (var j = 0; j <= i; j++) {
6 if (this[i] < this[j]) {
7 oValue = this[i];
8 this[i] = this[j];
9 this[j] = oValue;
10 }
11 }
12 }
13 return this;
14 };
1 // 数字数组由大到小排序
2 Array.prototype.Max2Min = function() {
3 var oValue;
4 for (var i = 0; i < this.length; i++) {
5 for (var j = 0; j <= i; j++) {
6 if (this[i] > this[j]) {
7 oValue = this[i];
8 this[i] = this[j];
9 this[j] = oValue;
10 }
11 }
12 }
13 return this;
14 };
1 // 获得数字数组中最大项
2 Array.prototype.GetMax = function() {
3 var oValue = 0;
4 for (var i = 0; i < this.length; i++) {
5 if (this[i] > oValue) {
6 oValue = this[i];
7 }
8 }
9 return oValue;
10 };
1 // 获得数字数组中最小项
2 Array.prototype.GetMin = function() {
3 var oValue = 0;
4 for (var i = 0; i < this.length; i++) {
5 if (this[i] < oValue) {
6 oValue = this[i];
7 }
8 }
9 return oValue;
10 };
1 // 获取当前时间的中文形式
2 Date.prototype.GetCNDate = function() {
3 var oDateText = '';
4 oDateText += this.getFullYear().LenWithZero(4) + new Number(24180).ChrW();
5 oDateText += this.getMonth().LenWithZero(2) + new Number(26376).ChrW();
6 oDateText += this.getDate().LenWithZero(2) + new Number(26085).ChrW();
7 oDateText += this.getHours().LenWithZero(2) + new Number(26102).ChrW();
8 oDateText += this.getMinutes().LenWithZero(2) + new Number(20998).ChrW();
9 oDateText += this.getSeconds().LenWithZero(2) + new Number(31186).ChrW();
10 oDateText += new Number(32).ChrW() + new Number(32).ChrW() + new Number(26143).ChrW() + new Number(26399).ChrW() + new String('26085199682010819977222352011620845').substr(this.getDay() * 5, 5).ToInt().ChrW();
11 return oDateText;
12 };
1 //扩展Date格式化
2 Date.prototype.Format = function(format) {
3 var o = {
4 "M+": this.getMonth() + 1, //月份
5 "d+": this.getDate(), //日
6 "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时
7 "H+": this.getHours(), //小时
8 "m+": this.getMinutes(), //分
9 "s+": this.getSeconds(), //秒
10 "q+": Math.floor((this.getMonth() + 3) / 3), //季度
11 "S": this.getMilliseconds() //毫秒
12 };
13 var week = {
14 "0": "\u65e5",
15 "1": "\u4e00",
16 "2": "\u4e8c",
17 "3": "\u4e09",
18 "4": "\u56db",
19 "5": "\u4e94",
20 "6": "\u516d"
21 };
22 if (/(y+)/.test(format)) {
23 format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
24 }
25 if (/(E+)/.test(format)) {
26 format = format.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[this.getDay() + ""]);
27 }
28 for (var k in o) {
29 if (new RegExp("(" + k + ")").test(format)) {
30 format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
31 }
32 }
33 return format;
34 }
35 Date.prototype.Diff = function(interval, objDate) {
36 //若参数不足或 objDate 不是日期类型則回传 undefined
37 if (arguments.length < 2 || objDate.constructor != Date) { return undefined; }
38 switch (interval) {
39 //计算秒差
40 case 's': return parseInt((objDate - this) / 1000);
41 //计算分差
42 case 'n': return parseInt((objDate - this) / 60000);
43 //计算時差
44 case 'h': return parseInt((objDate - this) / 3600000);
45 //计算日差
46 case 'd': return parseInt((objDate - this) / 86400000);
47 //计算周差
48 case 'w': return parseInt((objDate - this) / (86400000 * 7));
49 //计算月差
50 case 'm': return (objDate.getMonth() + 1) + ((objDate.getFullYear() - this.getFullYear()) * 12) - (this.getMonth() + 1);
51 //计算年差
52 case 'y': return objDate.getFullYear() - this.getFullYear();
53 //输入有误
54 default: return undefined;
55 }
56 };
1 //检测是否为空
2 Object.prototype.IsNullOrEmpty = function() {
3 var obj = this;
4 var flag = false;
5 if (obj == null || obj == undefined || typeof (obj) == 'undefined' || obj == '') {
6 flag = true;
7 } else if (typeof (obj) == 'string') {
8 obj = obj.trim();
9 if (obj == '') {//为空
10 flag = true;
11 } else {//不为空
12 obj = obj.toUpperCase();
13 if (obj == 'NULL' || obj == 'UNDEFINED' || obj == '{}') {
14 flag = true;
15 }
16 }
17 }
18 else {
19 flag = false;
20 }
21 return flag;
22 };