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("");
    }

  • 相关阅读:
    随机数测试
    往xml中更新节点
    Spring学习之代理
    SpringMVC基本配置
    Hibernate映射一对一关联关系
    成员变量的定义与使用
    面向对象三大特性
    请用心“品尝”网络电视精灵
    汽车租赁系统
    JSP 甜点
  • 原文地址:https://www.cnblogs.com/linsongbin/p/1374972.html
Copyright © 2011-2022 走看看