我不相信 Lua 是用 Ruby 或 Perl 之类的 split() 函数打包的。
我发现这家伙构建了一个 lua 版本的 Perl 的 split 函数:
http://lua-users.org/lists/lua-l/2011-02/msg01145.html
如果你能保证参数前面只有 1 个单词,并且那个单词不包含任何空格,你可以在该行中读取,在其上运行 split 函数,并使用返回数组的 1 索引值作为什么你想要的。
您也可以错误检查并确保在预期目录中获得“C:\”,或检查以确保字符串 == 到“On”或“Off”。由于使用硬编码的索引值,我真的提倡你错误检查你的期望值。没有什么比假设值错误更糟糕的了。
如果检测到错误,请务必将其记录或打印到屏幕上,以便您了解它。
这可以捕获可能输入的字符串不正确的错误。
一些简单的代码模拟了我建议你做的事情:
line = "directory C:\Program Files\abc\def/";
contents = line.split(" "); --Split using a space
directory = contents[2]; --Here is your directory
if(errorCheckDir(directory))
--Use directory
end
编辑:
Lua 下面的 cmets 确实从 1 开始索引,而不是 0。
此外,如果目录包含空格(可能)而不是简单地使用内容 [2],我将遍历除索引 1 之外的所有内容,并将目录拼凑在一起,确保在每个内容之间添加所需的空间您附加的索引。
因此,在上述情况下,contents[2] 和 contents[3] 必须重新缝合在一起,中间有一个空格才能恢复正确的目录。
目录 = 内容[2].." "..contents[3]
这可以很容易地使用一个有循环并返回正确目录的函数来自动化:
function recoverDir(contents)
directory = "";
--Recover the directory
for i=2, table.getn(contents) do
directory = directory..contents[i].." ";
end
--strip extra space on the end
dirEnd = string.len(directory);
directory = string.sub(directory,1,dirEnd-1);
return directory; --proper directory
end