卢阿分裂成文字

ano*_*non 9 string lua

我在lua中有一个字符串.

它是由一个数字(1个或多个)空格分隔的一堆[a-zA-Z0-9] +.

如何获取字符串并将其拆分为字符串表?

lhf*_*lhf 38

s="How do I take the string and split it into a table of strings?"
for w in s:gmatch("%S+") do print(w) end
Run Code Online (Sandbox Code Playgroud)

  • 参考手册gnome说:'%S`表示所有非空格字符. (8认同)
  • 接受的答案(ponzao)对问题中的规范是好的,但是更喜欢lhf的答案的原因是如果你有8位或多字节文本(所有非ascii),你仍然可以使用这个只在空格上正确分割方法. (8认同)

pon*_*zao 15

s = "foo bar 123"
words = {}
for word in s:gmatch("%w+") do table.insert(words, word) end
Run Code Online (Sandbox Code Playgroud)

  • 参考手册gnome说:`%w`代表所有字母数字字符. (6认同)
  • @PankajRawat,原来的问题指定了“ [a-zA-Z0-9] +”,其中不包含“ _”。您可能想要使用@lhf提供的答案。 (2认同)