Lua如何使用逗号拆分字符串

我需要用受限字符串(对我来说是逗号)或数组中的字符来分离字符串。在 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

如何格式化发送模式?

[其他备选方案]

如果可以的话?

objPropo = {}
str = "Maria Mercedez,,Jose,Sofia"
i = 1
for token in string.gmatch(str, ",") do
    objPropo[token] = token           <-------- 检查
    i = i + 1
end
native.showAlert("Names", objPropo["Maria Mercedez"], {"OK"})
native.showAlert("Names", objPropo["Jose"], {"OK"})

是正确的吗?

点赞
用户1009479
用户1009479

要将一个带有逗号的字符串分割,需要使用一个匹配非逗号的模式(后面跟着一个逗号):

for token in string.gmatch(str, "([^,]+),%s*") do
    objPropo[i] = token
    i = i + 1
end
2014-05-26 04:41:56