123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- -- 字符串转变为 table表
- local strtable = {}
- function strtable.StrToTable(str)
- -- if str == nil or type(str) ~= "string" then
- if str == nil then
- return
- end
- local z = {load("return " .. str)}
- return z
- end
- -- table表转换为str
- function strtable.ToStringEx(value)
- if type(value)=='table' then
- return strtable.TableToStr(value)
- elseif type(value)=='string' then
- return "\""..value.."\""
- else
- return tostring(value)
- end
- end
- --使用的时候是这个
- function strtable.TableToStr(t)
- if t == nil then return "" end
- if type(t) ~= 'table' then return t end
- local retstr= "{"
- local i = 1
- for key,value in pairs(t) do
- local signal = ","
- if i==1 then
- signal = ""
- end
- if key == i then
- retstr = retstr..signal..strtable.ToStringEx(value)
- else
- if type(key)=='number' or type(key) == 'string' then
- retstr = retstr..signal.."["..strtable.ToStringEx(key).."]="..strtable.ToStringEx(value)
- else
- if type(key)=='userdata' then
- retstr = retstr..signal.."*s"..TableToStr(getmetatable(key)).."*e".."="..strtable.ToStringEx(value)
- else
- retstr = retstr..signal..key.."="..strtable.ToStringEx(value)
- end
- end
- end
- i = i+1
- end
- retstr = retstr.."}"
- return retstr
- end
- return strtable
|