------------------ 原始邮件 ------------------
发件人: rechard1846 <recha...@gmail.com>
发送时间: 2018年1月3日 20:29
收件人: openresty <openresty@googlegroups.com>
主题: 回复:[openresty] string.find如何处理中文?
我在使用string.find切割字符串时,遇到了一个中文切割错误的问题。我的代码:
local function split_string(s, delim)
local start = 1
local t = {} -- results table
-- find each instance of a string followed by the delimiter
local i = 1
while true do
local pos = string.find(s, delim, start, true) -- plain find
if not pos then
break
end
t[i] = string.sub(s, start, pos - 1)
i = i + 1
start = pos + string.len (delim)
end -- while
t[i] = string.sub (s, start)
end
local str1 = '1~16硚口债~12' -- gbk编码
local ret = split_string(str1, '~')
期望的结果是ret为 1 16硚口债 12
但是实际结果为 1 16? 口债 12
'16硚口债'这个中文被拆开了,应该是他中间的字符让string.find匹配成了分隔符 ~
请问这种有什么好的办法处理吗? 求助~
--