WEB开发网
开发学院网页设计JavaScript JavaScript 字符串处理函数 阅读

JavaScript 字符串处理函数

 2010-09-14 13:35:06 来源:WEB开发网   
核心提示://截取字符串 包含中文处理function SubString(str, len, hasDot) {var newLength = 0;var newStr = "";var chineseRegex = /[^x00-xff]/g;var singleChar = "";v

//截取字符串 包含中文处理

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

Tags:JavaScript 字符串 处理

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接