JavaScript String 對象擴充方法

來源:互聯網
上載者:User
/** 在字串末尾追加字串 **/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);}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.