Created
December 27, 2019 17:14
-
-
Save vace/1abb6d8ebc873534ff056cb18a80add5 to your computer and use it in GitHub Desktop.
utf-8字符裁剪
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 slice_utf8str(str, maxLen) { | |
var len = str.length | |
var _len = 0 | |
var i = 0 | |
for (; i < len; i++) { | |
_len += str.charCodeAt(i) < 256 ? 1 : 2 | |
if (_len > maxLen) break | |
} | |
return str.slice(0, i) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment