어쩌다 해야 할 일이 있는데 기억력이 좋지 못해서 ...
1. UTF-8
- 0xxxxxxx - ASCII와 동일
- 11(0-1)xxxxx 10xxxxxx (10xxxxxx) - 2, 3 바이트 문자의 경우 첫 번째 바이트는 110 또는 1110으로 시작
- 4 바이트 표현도 존재하지만 자르는데 의미는 없으므로 생략.
2. 예제.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import re def cut_utf8_by_byte(text, length): """ """ if len(text) >= length: if re.match('[\xE0-\xFF]', text[length]) is not None: length -= 1 elif re.match('[\x80-\xFF]', text[length]) is not None: while True: length -= 1 if re.match('[\xDF-\xFF]', text[length]) is not None: break return text[:length] | cs |
댓글 없음:
댓글 쓰기