s = "this is a test string"
words = {}
for w in s:gmatch("%w+") do table.insert(words, w) end
使用此代码我能够分隔每个单词,但现在我需要能够只访问第n个单词。我怎么能打印第二个单词呢?我可以将它转换为数组然后使用类似于
的东西print words[2]
答案 0 :(得分:0)
像这样:
s = "this is a test string"
words = {}
for w in s:gmatch("%w+") do
table.insert(words, w)
end
print (words [2]) --> is
for k, v in ipairs (words) do
print (v)
end -- for
除了印刷“是”之外,还有以下内容:
is
a
test
string
print words[2]
您只能省略字符串文字和表格构造函数的括号,例如:
print "hello, world"
print ( table.getn { "the", "quick", "brown", "fox" } ) --> 4