javascript. String方法扩张.

有时候觉得js内置的方法不够用.可自己扩充.

下面是我扩充的几个方法.

为了避免变量冲出.覆盖.利用自执行函数实现.

+function (a) {
    a.fn = a.prototype;
    var fn = {
        /**
         * 进制转换
         * @param to
         * @returns {string}
         */
        toBits: function (to) {
            var bits = [];
            var t = this;
            var i = 0;
            for (i; i < t.length; i++) {
                var code = t.charCodeAt(i);
                bits.push(code.toString(to));
            }
            bits.reverse();
            return bits.join("");
        },
        /**
         * 去除右变指定字符串.默认空白
         * @returns {XML|string|void|*}
         */
        rtrim: function () {
            var replace = arguments[0] || "\\s";
            var r = new RegExp("(^" + replace + "{1,})");
            return this.replace(r, ‘‘);
        },
        /**
         * 去除左边指定字符串.默认空白
         * @returns {XML|string|void|*}
         */
        ltrim: function () {
            var replace = arguments[0] || "\\s";
            var r = new RegExp("(" + replace + "{1,}$)");
            return this.replace(r, ‘‘);
        },
        /**
         * 去除两边指定字符串.默认空白
         * @returns {XML|string|void|*}
         */
        trim: function () {
            var str = this, a = arguments;
            str = str.ltrim.apply(str.rtrim.apply(str, a), a);
            return str;
        },
        str_repeat: function (i, m) {
            for (var o = []; m > 0; o[--m] = i);
            return o.join(‘‘);
        },
        /**
         * c的sprintf实现
         * @returns {string}
         */
        sprintf: function () {
            var i = 0, a, f = this.toString(), o = [], m, p, c, x, s = ‘‘;
            while (f) {
                if (m = /^[^\x25]+/.exec(f)) {
                    o.push(m[0]);
                }
                else if (m = /^\x25{2}/.exec(f)) {
                    o.push(‘%‘);
                }
                else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|‘[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
                    if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) {
                        throw(‘Too few arguments.‘);
                    }
                    if (/[^s]/.test(m[7]) && (typeof(a) != ‘number‘)) {
                        throw(‘Expecting number but found ‘ + typeof(a));
                    }
                    switch (m[7]) {
                        case ‘b‘:a = a.toString(2);break;
                        case ‘c‘:a = this.fromCharCode(a); break;
                        case ‘d‘:a = parseInt(a);break;
                        case ‘e‘:a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
                        case ‘f‘:a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a);break;
                        case ‘o‘:a = a.toString(8);break;
                        case ‘s‘:a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a);break;
                        case ‘u‘:a = Math.abs(a);break;
                        case ‘x‘:a = a.toString(16); break;
                        case ‘X‘:a = a.toString(16).toUpperCase();break;
                    }
                    a = (/[def]/.test(m[7]) && m[2] && a >= 0 ? ‘+‘ + a : a);
                    c = m[3] ? m[3] == ‘0‘ ? ‘0‘ : m[3].charAt(1) : ‘ ‘;
                    x = m[5] - String(a).length - s.length;
                    p = m[5] ? this.str_repeat(c, x) : ‘‘;
                    o.push(s + (m[4] ? a + p : p + a));
                }
                else {
                    throw(‘Huh ?!‘);
                }
                f = f.substring(m[0].length);
            }
            return o.join(‘‘);
        }






    };
    for (var i in fn) {
        a.fn[i] = fn[i];
    }
    window.String = a;
}(String);

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。