Created
January 20, 2013 12:51
-
-
Save supersheep/4578429 to your computer and use it in GitHub Desktop.
截字
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 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