Skip to content

Instantly share code, notes, and snippets.

@yangjunjun
Created December 6, 2013 05:58
Show Gist options
  • Save yangjunjun/7819243 to your computer and use it in GitHub Desktop.
Save yangjunjun/7819243 to your computer and use it in GitHub Desktop.
获取指定个数字符串(汉字字符长度为2)
function getStr(str, len){
var len = 0;
for (var i=0; i<str.length; i++) {
var c = str.charCodeAt(i);
if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {
len++;
}
else {
len+=2;
}
if(len > len){
return str.substring(0,i) + '...';
}
}
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment