Lua tableToString


-- 将table转换成字符串输出,值为nil的字段不会输出

-- @ tb : 待转换table @fmt : 是否格式化输出结果 @lv : 当前为table中第几层,不用管 @ tb_exist : 不用管

function tableToString (tb, fmt, lv, tb_exist)

    if type(tb) ~= "table" then return false end

    lv = lv or 1

    tb_exist = tb_exist or {} -- 用来记录已解析过的table,防止相互引用导致死循环,暂不想实现

    local space = "\t"

    local new_line = "\n"

    if fmt == nil or fmt == false then

        space = ""

        new_line = ""

    end

    local sort_tb = {}

    local final_text = ""

    local function keyToString(key)

        if type(key) == "number" then

            return "[ " .. key .. " ] = "

        elseif type(key) == "string" then

            return "['" .. key .. "'] = "

        end

    end

    for k in pairs(tb) do

        local tb_k = {}

        tb_k.key = k

        tb_k.value = tb[k]

        tb_k.text = new_line

        for s = 1, lv do -- 在前面添加 lv个空格

            tb_k.text = tb_k.text .. space

        end

        if type(tb_k.value) == "function" then -- 或者不用这个,直接在else里面自动tostring成function

            tb_k.text = (tb_k.text) .. keyToString(tb_k.key) .. "function()"

        elseif type(tb_k.value) == "table" then

            tb_k.text = (tb_k.text) .. keyToString (tb_k.key) .. tableToString(tb_k.value, fmt, lv + 1, tb_exist)

        elseif type(tb_k.value) == "string" then

            tb_k.text = (tb_k.text) .. keyToString(tb_k.key) .. "\"" .. tb_k.value .. "\""

        elseif type(tb_k.value) == "number" then

            tb_k.text = (tb_k.text) .. keyToString(tb_k.key) .. tostring(tb_k.value)

            -- 在数字后加上16进制显示

            -- tb_k.text = tbk.text .. string.format("\t\t\t0x%08x ", tb_k.value)

        else --"number" 、 "boolean" 等

            tb_k.text = (tb_k.text) .. keyToString(tb_k.key) .. tostring(tb_k.value)

        end

        table.insert(sort_tb, tb_k)

    end

    -- 排个序

    table.sort(sort_tb, function(a, b) return type(a.key) < type(b.key) end)

    for i = 1, #sort_tb do -- 连接当前table中的所有字符串

        final_text = final_text .. (sort_tb[i].text)

        if i < #sort_tb then final_text = final_text .. "," end

    end

    -- 添加后面的大括号

    if lv == 1 then

        final_text = "table = {" .. final_text .. new_line .. "}"

    else

        if #sort_tb > 0 then

            final_text = final_text .. new_line

            for l = 1, lv - 1 do

                final_text = final_text .. space

            end

        end

        final_text = "{" .. final_text .. "}"

    end

    return final_text

end

使用方法:


local test_tb = {

1,"text",function()end,true,nil,{},

a = 1,b = "text", c = function()end,d = true, e = nil,f = {"text",a = {nil}}

}

print(tableToString(test_tb,true))

效果图

image

print(tableToString(test_tb))

~~~

image
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,680评论 1 32
  • 专业考题类型管理运行工作负责人一般作业考题内容选项A选项B选项C选项D选项E选项F正确答案 变电单选GYSZ本规程...
    小白兔去钓鱼阅读 10,642评论 0 13
  • 转载自VR设计云课堂[https://www.jianshu.com/u/c7ffdc4b379e]Unity S...
    水月凡阅读 1,177评论 0 0
  • | java.math |--java.math.BigDecimal |--java.math.BigInteg...
    flyrae阅读 2,977评论 0 2
  • 2018年第十六周,从2018年4月16日到22日。以后每周的检视固定格式,暂时先从收获和不足两个方面进行检视。 ...
    一汪青水阅读 207评论 2 1

友情链接更多精彩内容