技术开发 频道

在Lua表中使用gfind迭代字符串

  【IT168 技术文档】这是一个更简洁的迭代字符串的方法.

  for color in string.gfind("red green blue black orange green", "[^ ]+") do   ChatFrame1:AddMessage('My favorite color is ' .. color .. '.');   end

  第二个参数以 '^' 开头的是分隔符. 这里表示非空白, 你可以使用分隔符 '\n' 来迭代多行的长字符串.

  HELP_MESSAGE = [[   This is the first line of help to show.   And here is the second.   And here is the third.   Let's make sure to include enough text to bore the user.   ]];   for line in string.gfind(HELP_MESSAGE, '[^\n]+') do   ChatFrame1:AddMessage(line);   end
0
相关文章