lua 索引取字符串的值

2017-06-20  本文已影响211人  zero_0
getmetatable("").__index = function(str, i)
    return string.sub(str,i,i)
end 

---test
local array = {
    ['0']='零',
    ['1']='一',
    ['2']='二',
    ['3']='三',
    ['4']='四',
    ['5']='五',
    ['6']='六',
    ['7']='七',
    ['8']='八',
    ['9']='九',
    ['+'] = '加',
    ['-'] = '减',
}

local n = -9876543210
local s = tostring(n)
local result = ""
for i = 1, #s do
    local index = s[i]
    local c = array[index]
    result=result..c
end
print(type(result), result)

---out
string  减九八七六五四三二一零
上一篇下一篇

猜你喜欢

热点阅读