lua字符串分割函數

-------------------------------------------------------
--lua 字符串分割函數
--參數:待分割的字符串,分割字符
--返回:子串表.(含有空串)
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            
			local size_t = table.getn(sub_str_tab)
			table.insert(sub_str_tab,size_t+1,str);
			break;  
		end
		local sub_str = string.sub(str, 1, pos - 1);              
		local size_t = table.getn(sub_str_tab)
		table.insert(sub_str_tab,size_t+1,sub_str);
		local t = string.len(str);
		str = string.sub(str, pos + 1, t);   
	end    
	return sub_str_tab;
end

local str = "460361|ebx4509441z888|78613585"
local ta  = lua_string_split(str,"|")

local size = table.getn(ta)
for i = 1,size ,1 do
	print(ta[i])
end
在lua中沒有發現字符串分割的函數,自己寫了一個,記錄在此,以備使用。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章