在Lua中拆分字符串,保存在2个变量中

时间:2013-11-24 20:01:06

标签: string lua split division

我有一个字符串,即= s = "Pedro Martinez, Defense"

我想在逗号之前和逗号之后拆分字符串,将这些剪辑存储在2个变量上,例如:

我认为我需要使用string.gmatch函数或string.sub

我该怎么做?

1 个答案:

答案 0 :(得分:2)

How to convert GPS coordinates to decimal in Lua?上接受的答案显示了如何使用string.match和模式。应用与那里提到的相同的技术,你可以使用

local name, expertise = string.match(s, "(.*),%s*(%a*)")

这将导致名称为“Pedro Martinez”,专业知识为“防御”。