Created
December 6, 2013 05:58
-
-
Save yangjunjun/7819243 to your computer and use it in GitHub Desktop.
获取指定个数字符串(汉字字符长度为2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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