Skip to content

Instantly share code, notes, and snippets.

@guerbai
Last active June 2, 2019 03:59
Show Gist options
  • Save guerbai/6f0321a892bc8cff6fd791462977ec0a to your computer and use it in GitHub Desktop.
Save guerbai/6f0321a892bc8cff6fd791462977ec0a to your computer and use it in GitHub Desktop.
获取包括汉字在内的字符串长度
# -*- 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