-
-
Save neilwong2012/14f3e7a560f2c1ab17fd to your computer and use it in GitHub Desktop.
function.js
This file contains hidden or 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 cutstr(str, len) { | |
var str_length = 0; | |
var str_len = 0; | |
str_cut = new String(); | |
str_len = str.length; | |
for (var i = 0; i < str_len; i++) { | |
a = str.charAt(i); | |
str_length++; | |
if (escape(a).length > 4) { | |
//中文字符的长度经编码之后大于4 | |
str_length++; | |
} | |
str_cut = str_cut.concat(a); | |
if (str_length >= len) { | |
str_cut = str_cut.concat("..."); | |
return str_cut; | |
} | |
} | |
//如果给定字符串小于指定长度,则返回源字符串; | |
if (str_length < len) { | |
return str; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment