我是靠谱客的博主 眼睛大白云,这篇文章主要介绍Lua字符串中的中英文分割处理,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
-- 计算 UTF8 字符串的长度,每一个中文算一个字符 -- @function [parent=#string] utf8len -- @param string input 输入字符串 -- @return integer#integer 长度 --[[-- 计算 UTF8 字符串的长度,每一个中文算一个字符 ~~~ lua local input = "你好World" print(string.utf8len(input)) -- 输出 7 ~~~ ]] -- end -- function string.utf8len(input) local len = string.len(input) local left = len local cnt = 0 local arr = {0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc} while left ~= 0 do local tmp = string.byte(input, -left) local i = #arr while arr[i] do if tmp >= arr[i] then left = left - i break end i = i - 1 end cnt = cnt + 1 end return cnt end -- lzh -- 功能:将字符串拆成单个字符,存在一个table中 function string.utf8tochars(input) local list = {} local len = string.len(input) local index = 1 local arr = {0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc} while index <= len do local c = string.byte(input, index) local offset = 1 if c < 0xc0 then offset = 1 elseif c < 0xe0 then offset = 2 elseif c < 0xf0 then offset = 3 elseif c < 0xf8 then offset = 4 elseif c < 0xfc then offset = 5 end local str = string.sub(input, index, index+offset-1) -- print(str) index = index + offset table.insert(list, {byteNum = offset, char = str}) end return list end ---------应用的例子--------- -- 函数功能:字符处理 -- 超30个英文字符长度,后面的用...代替 function Test:ProcessEmailName(content) local chars = string.utf8tochars(content) local nums = 0 local newString = "" for i,v in ipairs(chars or {}) do if v.byteNum>=3 then nums = nums + 2 else nums = nums + 1 end -- 限制超过30个英文字符的长度 -- printLog(nums) if nums <= 30 then newString = newString .. v.char else newString = newString .. "..." break end end return newString end

最后

以上就是眼睛大白云最近收集整理的关于Lua字符串中的中英文分割处理的全部内容,更多相关Lua字符串中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部