使用string.gmatch
local plan_id = "1,2,3,4,55,66"
local match = string .gmatch( plan_id,"%d+" )
local plan_ids = {}
for v in match do
table .insert(plan_ids,v)
end
print (#plan_ids)
print (plan_ids[2 ])
6 2
使用string.sub
function 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;
end
local plan_id = 11
local plan_ids = split(plan_id,"," )
if #(plan_ids) == 1 then
print ("t" )
else
print ("f" )
end
local plan_id = "11,33,44,55,66,77"
local plan_ids = split(plan_id,"," )
print (plan_ids[5 ])
t 66