Last active
June 2, 2019 03:59
-
-
Save guerbai/6f0321a892bc8cff6fd791462977ec0a 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
# -*- coding: utf-8 -*- | |
# 一个汉字算两个字符 | |
def _chars_fit_baohong_limit(element): | |
element_len = 0 | |
for index, char in enumerate(unicode(element)): | |
if element_len >= BAOHONG_TEXT_LENGTH_LIMIT: | |
index -= 2 | |
break | |
if u'\u4e00' <= char <= u'\u9fff': # 是汉字. | |
element_len += 2 | |
else: | |
element_len += 1 | |
return unicode(element)[:index+1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment