--我用Lua的改进实现 AutoIt 中 StringSplit 函数的功能 (支持Lua的正则表达式)
function StringSplit(string,split)
local list = {}
local pos = 1
if string.find("", split, 1) then -- this would result in endless loops
error("split matches empty string!")
end
while 1 do
local first, last = string.find(string, split, pos)
if first then -- found?
table.insert(list, string.sub(string, pos, first-1))
pos = last+1
else
table.insert(list, string.sub(string, pos))
break
end
end
return list
end
function print_contents(list)
for _,v in pairs(list) do
print(v)
end
end
t="c://windows//as.bmp//ccc.bmp"
tt=StringSplit(t,"//")
--打印tt的的元素数
print(#tt)
--打印分割后的元素数
print_contents(tt)
--说明:本StringSplit函数支持Lua的正则表达式例子如下,比如split参数是个可能含有空格也可能不含有空格的情形如下处理
local list1 = StringSplit("c:// windows//Program Files// ccc.bmp","%s*%//%s*")
print_contents(list1)
--还可以有更多的应用,比如用来把获得的其他程序的快捷键字串"Ctrl+ Alt +F3"变成AutoHotkey支持的格式 ^!F3