s = "this is a test string"
words = {}
for w in s:gmatch("%w+") do table.insert(words, w) end使用这段代码,我能够将每个单词分开,但现在我需要能够访问第n个单词。例如,我如何打印第二个单词?我能否以某种方式将其转换为数组,然后使用类似于
print words[2]发布于 2016-02-07 07:19:15
如下所示:
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“之外,它还遵循以下内容:
is
a
test
string打印words2
只能省略字符串文本和表构造函数的括号,如下所示:
print "hello, world"
print ( table.getn { "the", "quick", "brown", "fox" } ) --> 4https://stackoverflow.com/questions/26262553
复制相似问题