我需要使用有限字符串(对于我逗号)或数组中的char来分隔字符串。如何分离o在Lua中用逗号分隔。
我查看了这些链接,但我不明白:
http://lua-users.org/wiki/SplitJoin
http://lua-users.org/wiki/PatternsTutorial
https://stackoverflow.com/questions/1426954/split-string-in-lua
objPropo = {}
str = "Maria Mercedez,,Jose,Sofia"
i = 1
for token in string.gmatch(str, ",") do
objPropo[i] = token
i = i + 1
end
native.showAlert("Names", objPropo[1], {"OK"})
native.showAlert("Names", objPropo[2], {"OK"}) <-- Is this error? Because is nil? or what happend?
native.showAlert("Names", objPropo[3], {"OK"})
native.showAlert("Names", objPropo[4], {"OK"})
它可以显示:
Maria Mercedez
formatt如何发送模式?
[其他选择]
如果有可能吗?
objPropo = {}
str = "Maria Mercedez,,Jose,Sofia"
i = 1
for token in string.gmatch(str, ",") do
objPropo[token] = token <-------- CHECK
i = i + 1
end
native.showAlert("Names", objPropo["Maria Mercedez"], {"OK"})
native.showAlert("Names", objPropo["Jose"], {"OK"})
是对的吗?
答案 0 :(得分:4)
要使用逗号分割字符串,您需要使用与非逗号匹配的模式(后跟逗号):
for token in string.gmatch(str, "([^,]+),%s*") do
objPropo[i] = token
i = i + 1
end