-- @function Str_split 字符串分割函数
-- @param str 待分割的字符串
-- @param splitStr 用于分割字符串的分隔符
-- @return subStrTable 分割后的Table
function Str_split(str, splitStr)
local subStrTable = {};
while (true) do
local pos = string.find(str, splitStr);
if (not pos) then
if str ~= "" then
subStrTable[#subStrTable + 1] = str;
end
break;
end
local subStr = string.sub(str, 1, pos - 1);
if subStr ~= "" then
subStrTable[#subStrTable + 1] = subStr;
end
str = string.sub(str, pos + string.len(splitStr), #str);
end
return subStrTable;
end
--- example
local subStrTable= Str_split("1|22|333|4444|55555", "|")
for i = 1, #subStrTable do
print(subStrTable[i])
end
---output
1
22
333
4444
55555
lua根据分隔符把String转为Table
于 2023-04-03 22:29:00 首次发布