ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

utf8字符串处理 utf8Ext

2021-11-12 09:34:33  阅读:144  来源: 互联网

标签:end string utf8 assert utf8Ext 字符串 return local utf8StringLen


 

 1 function string.utf8CharSize(c)
 2     if not c then return 0 end
 3     if c > 240 then return 4 end
 4     if c > 225 then return 3 end
 5     if c > 192 then return 2 end
 6     return 1
 7 end
 8 
 9 function string.utf8StringLen(str)
10     if nil == str or "" == str then return 0 end
11     assert("string" == type(str), "not string")
12     
13     local len = 0
14     local i = 1
15     while i <= #str do
16         local b = string.byte(str, i)
17         i = i + string.utf8CharSize(b)
18         len = len + 1
19     end
20     return len
21 end
22 
23 function string.utf8StringSub(str, index, count)
24     if nil == index then index = 1 end
25     if nil == count then count = string.utf8StringLen(str) end
26     
27     local byteCount = #str
28     local i = 1
29     --跳过起始的n个
30     while index > 1 and i <= byteCount do
31         local b = string.byte(str, i)
32         i = i + string.utf8CharSize(b)
33         index = index - 1
34     end
35     
36     local j = i
37     --子串结束位置
38     while count > 0 and j <= byteCount do
39         local b = string.byte(str, j)
40         j = j + string.utf8CharSize(b)
41         count = count - 1
42     end
43     
44     return str:sub(i, j-1)
45 end
46 
47 function string.utf8CharArray(str, index, count, dstTb, dstIndex)
48     if nil == index then index = 1 end
49     if nil == count then count = string.utf8StringLen(str) end
50     if nil == dstTb then dstTb = {} end
51     
52     local byteCount = #str
53     local i = 1
54     --跳过起始的n个
55     while index > 1 and i <= byteCount do
56         local b = string.byte(str, i)
57         i = i + string.utf8CharSize(b)
58         index = index - 1
59     end
60     
61     local j = i
62     local leftCount = count
63         --子串结束位置
64         if nil == dstIndex then
65             while leftCount > 0 and j <= byteCount do
66             local b = string.byte(str, j)
67             local size = string.utf8CharSize(b)
68             local utf8Char = str:sub(j, j+size-1)
69             table.insert(dstTb, utf8Char)
70             j = j + size
71             leftCount = leftCount - 1
72         end
73     else
74         while leftCount > 0 and j <= byteCount do
75             local b = string.byte(str, j)
76             local size = string.utf8CharSize(b)
77             local utf8Char = str:sub(j, j+size-1)
78             dstTb[dstIndex] = utf8Char
79             j = j + size
80             leftCount = leftCount - 1
81             dstIndex = dstIndex + 1
82         end
83     end
84     
85     return dstTb, count
86 end

 

测试代码:

 1 local function Test1()
 2     assert(6 == string.utf8StringLen("123abc"))
 3     assert(2 == string.utf8StringLen("中文"))
 4     assert(4 == string.utf8StringLen("中文1a"))
 5     assert(5 == string.utf8StringLen(".,;<>"))
 6     assert(5 == string.utf8StringLen("。,;《》"))
 7     assert(10 == string.utf8StringLen(".,;<>。,;《》"))
 8     
 9     assert("文1" == string.utf8StringSub("中文1a", 2, 2))
10     assert("文啊" == string.utf8StringSub("中文啊呀", 2, 2))
11     
12     local tb = string.utf8CharArray("中文啊呀", 2, 2)
13     assert("文" == tb[1])
14     assert("啊" == tb[2])
15 end
16 
17 Test1()

 

标签:end,string,utf8,assert,utf8Ext,字符串,return,local,utf8StringLen
来源: https://www.cnblogs.com/sailJs/p/15542671.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有