<script>
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;
}
alert(subString("js截取中英文字符串无乱码",10,true));
</script> 

相关文章:

  • 2022-12-23
  • 2021-10-31
  • 2021-12-16
  • 2022-12-23
  • 2022-01-22
  • 2021-08-12
  • 2022-12-23
  • 2021-09-13
猜你喜欢
  • 2021-10-28
  • 2022-12-23
  • 2022-12-23
  • 2022-03-09
  • 2022-12-23
  • 2021-11-21
  • 2021-06-10
相关资源
相似解决方案