美文网首页
lua 索引取字符串的值

lua 索引取字符串的值

作者: zero_0 | 来源:发表于2017-06-20 23:34 被阅读211次
    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  减九八七六五四三二一零
    

    相关文章

      网友评论

          本文标题:lua 索引取字符串的值

          本文链接:https://www.haomeiwen.com/subject/xazaqxtx.html