Skip to content

Instantly share code, notes, and snippets.

@supersheep
Created January 20, 2013 12:51
Show Gist options
  • Save supersheep/4578429 to your computer and use it in GitHub Desktop.
Save supersheep/4578429 to your computer and use it in GitHub Desktop.
截字
// 非常偷懒的假设中文字符集为两个字符长度,别的都是一个
function clipString(str,length){
var strArr = str.split("");
var result = [];
for(var i=0;chr=strArr[i];i++){
if(/[\u0391-\uFFE5]/.test(chr)){
length -= 2;
}else{
length -= 1;
}
result.push(chr);
if(length <= 0){
return result.join("");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment