lua 实现tableToString

function tableToString(studentNum)

     local str =  "{ "
     str = str.."\n"
     for k, v in pairs(studentNum) do
        if type(v) ~= "table" then
            str = str.."[\""..k.."\"]"
            str = str..":"
            str = str..v
            str = str.."," 
            str = str.."\n" 
        else 
            str = str.."[\""..k.."\"]"
            str = str..":"
            str = str..tableToString(v)
            str = str.."," 
            str = str.."\n"
        end
     end
     str = string.sub(str, 1, -3)
     str = str.."\n"
     str = str .." }"
     return str
    -- print("tableToString===", str)
end

local studentNum = {1, {6, 7, {"hello", "world"}}, 5}
local str = tableToString(studentNum)
print(str)

  

posted @ 2017-11-04 14:21  xshang  阅读(3015)  评论(0编辑  收藏  举报