lua 从文件中读取字符串
function ReadFile(par)
local ret = nil;
local path = par;
local f = io.open(path, "r");
if f == nil then return nil, "failed to open file"; end
ret = f:read("*all");
f:close();
return ret;
end
写字符串入文件
function WriteFile(par,str)
local ret = nil;
local path = par;
local f = io.open(path, "w");
if f == nil then return nil, "failed to open file"; end
ret = f:write(tostring(str));
f:close();
end
添加内容入文件
function UpdateFile(par,str)
local ret = nil;
local path = par;
local f = io.open(path, "a+");
if f == nil then return nil, "failed to open file"; end
ret = f:write(tostring(str));
f:close();
end
分隔符分割
-- 参数:待分割的字符串,分割字符
-- 返回:子串表.(含有空串)
function lua_string_split(str, split_char)
local sub_str_tab = {};
while (true) do
local pos = string.find(str, split_char);
if (not pos) then
sub_str_tab[#sub_str_tab + 1] = str;
break;
end
local sub_str = string.sub(str, 1, pos - 1);
sub_str_tab[#sub_str_tab + 1] = sub_str;
str = string.sub(str, pos + 1, #str);
end
return sub_str_tab;
--首先寻找分隔符,没有的话将str放入表中
--找到后分隔符前的放入表中,str等于分隔符后的字符串
--重复上述步骤至没有分隔符为止