码迷,mamicode.com
首页 > 其他好文 > 详细

lua字符串分割 string.split实现

时间:2015-04-27 21:54:29      阅读:942      评论:0      收藏:0      [点我收藏+]

标签:

function Split(szFullString, szSeparator)
local nFindStartIndex = 1
local nSplitIndex = 1
local nSplitArray = {}
while true do
   local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
   if not nFindLastIndex then
    nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
    break
   end
   nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
   nFindStartIndex = nFindLastIndex + string.len(szSeparator)
   nSplitIndex = nSplitIndex + 1
end
return nSplitArray

--ceshi
local currenttime = os.date(); --"04/23/2015 16:50:46" ;--= os.date();
print(os.date());
local cur_list = Split(currenttime," ");
		local cur_date_str = string.format("%s",cur_list[1]);
		local cur_time_str = string.format("%s",cur_list[2]);
		local cur_time_list = Split(cur_time_str,":");
		local cur_data_list = Split(cur_date_str,"/");

		print(cur_date_str)
		print(cur_time_str)

for i = 1, #cur_data_list
do
	str = string.format("index %d: value = %s", i, cur_data_list[i]);
	print(str);
end

for i = 1, #cur_time_list
do
	str = string.format("index %d: value = %s", i, cur_time_list[i]);
	print(str);
end


原文查看:http://www.seanyxie.com/lua%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%88%86%E5%89%B2-string-split%E5%AE%9E%E7%8E%B0/



lua字符串分割 string.split实现

标签:

原文地址:http://blog.csdn.net/seanyxie/article/details/45315223

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!