-- 字符串的数组形式读访问,未作参数校验和错误处理
function string2array()
local s = ''
local s_mt = getmetatable(s)
local _ndx = s_mt.__index
s_mt.__index = function(s, i)
if type(i) == 'number' then
return string.sub(s, i, i)
else
return _ndx[i] -- s[i]
end
end
end
string2array()
local s = 'abc'
print(s[2])
阅读(1191) | 评论(0) | 转发(0) |