/** 在字串末尾追加字串 **/String.prototype.append = function (str) { return this.concat(str);}/** 刪除指定索引位置的字元,索引無效將不刪除任何字元 **/String.prototype.deleteCharAt = function (index) { if (index < 0 || index >= this.length) { return this.valueOf(); } else if (index == 0) { return this.substring(1, this.length); } else if (index == this.length - 1) { return this.substring(0, this.length - 1); } else { return this.substring(0, index) + this.substring(index + 1); }}/** 刪除指定索引區間的字串 **/String.prototype.deleteString = function (start, end) { if (start == end) { return this.deleteCharAt(start); } else { if (start > end) { var temp = start; start = end; end = temp; } if (start < 0) { start = 0; } if (end > this.length - 1) { end = this.length - 1; } return this.substring(0, start) + this.substring(end +1 , this.length); }}/** 檢查字串是否以subStr結尾 **/String.prototype.endWith = function (subStr) { if (subStr.length > this.length) { return false; } else { return (this.lastIndexOf(subStr) == (this.length - subStr.length)) ? true : false; }}/** 比較兩個字串是否相等,也可以直接用 == 進行比較 **/String.prototype.equal = function (str) { if (this.length != str.length) { return false; } else { for (var i = 0; i < this.length; i++) { if (this.charAt(i) != str.charAt(i)) { return false; } } return true; }}/** 比較兩個字串是否相等,不區分大小寫 **/String.prototype.equalIgnoreCase = function (str) { var temp1 = this.toLowerCase(); var temp2 = str.toLowerCase(); return temp1.equal(temp2);}/** 將指定的字串插入到指定的位置後面,索引無效將直接追加到字串的末尾 **/String.prototype.insert = function (ofset, subStr) { if (ofset < 0 || ofset >= this.length - 1) { return this.append(subStr); } return this.substring(0, ofset + 1) + subStr + this.substring(ofset + 1);}/** 判斷字串是否數字串 **/String.prototype.isAllNumber = function () { for (var i = 0; i < this.length; i++) { if (this.charAt(i) < '0' || this.charAt(i) > '9') { return false; } } return true;}/** 將字串反序排列 **/String.prototype.reserve = function () { var temp = ""; for (var i = this.length - 1; i >= 0; i--) { temp = temp.concat(this.charAt(i)); } return temp;}/** 將指定的位置的字元設定為另外指定的字元或字串.索引無效將直接返回不做任何處理 **/String.prototype.setCharAt = function (index, subStr) { if (index < 0 || index > this.length - 1) { return this.valueOf(); } return this.substring(0, index) + subStr + this.substring(index+1);}/** 檢查字串是否以subStr開頭 **/String.prototype.startWith = function (subStr) { if (subStr.length > this.length) { return false; } return (this.indexOf(subStr) == 0) ? true : false;}/** 計算長度,每個漢字佔兩個長度,英文字元每個佔一個長度 **/String.prototype.charLength = function () { var temp = 0; for (var i = 0; i < this.length; i++) { if (this.charCodeAt(i) > 255) { temp += 2; } else { temp += 1; } } return temp;}String.prototype.charLengthReg = function () { return this.replace(/[^\x00-\xff]/g, "**").length;}/** 去掉首尾空格 **/String.prototype.trim = function () { return this.replace(/(^\s*)|(\s*$)/g, "");}/** 測試是否是數字 **/String.prototype.isNumeric = function () { var tmpFloat = parseFloat(this); if (isNaN(tmpFloat)) return false; var tmpLen = this.length - tmpFloat.toString().length; return tmpFloat + "0".Repeat(tmpLen) == this;}/** 測試是否是整數 **/ String.prototype.isInt = function () { if (this == "NaN") return false; return this == parseInt(this).toString();}/** 擷取N個相同的字串 **/String.prototype.Repeat = function (num) { var tmpArr = []; for (var i = 0; i < num; i++) tmpArr.push(this); return tmpArr.join("");}/** 合并多個空白為一個空白 **/ String.prototype.resetBlank = function () { return this.replace(/s+/g, " ");}/** 除去左邊空白 **/ String.prototype.LTrim = function () { return this.replace(/^s+/g, "");}/** 除去右邊空白 **/ String.prototype.RTrim = function () { return this.replace(/s+$/g, "");}/** 除去兩邊空白 **/ String.prototype.trim = function () { return this.replace(/(^s+)|(s+$)/g, "");}/** 保留數字 **/ String.prototype.getNum = function () { return this.replace(/[^d]/g, "");}/** 保留字母 **/ String.prototype.getEn = function () { return this.replace(/[^A-Za-z]/g, "");}/** 保留中文 **/ String.prototype.getCn = function () { return this.replace(/[^u4e00-u9fa5uf900-ufa2d]/g, "");}/** 得到位元組長度 **/ String.prototype.getRealLength = function () { return this.replace(/[^x00-xff]/g, "--").length;}/** 從左截取指定長度的字串 **/ String.prototype.left = function (n) { return this.slice(0, n);}/** 從右截取指定長度的字串 **/ String.prototype.right = function (n) { return this.slice(this.length - n);}