Skip to content

Instantly share code, notes, and snippets.

@vace
Created December 27, 2019 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vace/1abb6d8ebc873534ff056cb18a80add5 to your computer and use it in GitHub Desktop.
Save vace/1abb6d8ebc873534ff056cb18a80add5 to your computer and use it in GitHub Desktop.
utf-8字符裁剪
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