문제

My string is

text1,text2

I want to split text1 and text2 by using the ','.

도움이 되었습니까?

해결책 2

To get an iterator with the substrings, you can call string.gmatch.

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

To just get them into two separate strings, you can just call the iterator;

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

If you want them stored in an array instead, there's a whole discussion here how to achieve that.

@lhf added a better pattern [^,]+ in the comments, mine splits on any punctuation, his only on comma.

다른 팁

Try this:

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

Try the functions given in this page:

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top