我需要从一个字符中分割字符串

时间:2013-05-24 15:08:54

标签: lua love2d

我的字符串是

text1,text2

我想使用','。

拆分text1和text2

3 个答案:

答案 0 :(得分:3)

试试这个:

s="text1,text2"
t1,t2=s:match("(.-),(.-)$")
print(t1,t2)

答案 1 :(得分:0)

要获得带有子串的迭代器,可以调用string.gmatch

for i in string.gmatch(example, "%P+") do
  print(i)
end

要将它们分成两个单独的字符串,您只需调用迭代器;

> iter = string.gmatch(indata, "%P+")
> str1 = iter()
> str2 = iter()
> print (str1)
test1
> print (str2)
test2

如果你希望它们存储在一个数组中,那么就会有一个完整的讨论here如何实现它。

@lhf在评论中添加了一个更好的模式[^,]+,在任何标点符号上都有我的分裂,他只在逗号上。

答案 2 :(得分:0)

尝试本页中给出的功能:

http://lua-users.org/wiki/SplitJoin

相关问题