zoukankan      html  css  js  c++  java
  • JavaScript 字符串处理函数

    //截取字符串 包含中文处理
    function SubString(str, len, hasDot) {
        var newLength = 0;
        var newStr = "";
        var chineseRegex = /[^\x00-\xff]/g;
        var singleChar = "";
        var strLength = str.replace(chineseRegex, "**").length;
        for (var i = 0; i < strLength; i++) {
            singleChar = str.charAt(i).toString();
            if (singleChar.match(chineseRegex) != null) {
                newLength += 2;
            }
            else {
                newLength++;
            }
            if (newLength > len) {
                break;
            }
            newStr += singleChar;
        }

        if (hasDot && strLength > len) {
            newStr += "...";
        }
        return newStr;
    }

    //字符串连接
    function StringBuffer() {
        this._string = new Array;
    }

    StringBuffer.prototype.append = function(str) {
        return this._string.push(str);
    }

    StringBuffer.prototype.toString = function() {
        return this._string.join("");
    }

  • 相关阅读:
    Mysql任务调度
    使用 IntraWeb (18)
    使用 IntraWeb (17)
    替盛大代发的招聘启示
    使用 IntraWeb (16)
    使用 IntraWeb (15)
    使用 IntraWeb (14)
    使用 IntraWeb (13)
    使用 IntraWeb (12)
    使用 IntraWeb (11)
  • 原文地址:https://www.cnblogs.com/linsongbin/p/1374972.html
Copyright © 2011-2022 走看看