【问题标题】:How do I split a string on a certain character in lua? [duplicate]如何在lua中的某个字符上拆分字符串? [复制]
【发布时间】:2020-12-03 14:53:48
【问题描述】:

我正在尝试在 Lua 中的空间上拆分字符串。我的意思是如果我有一个名为“str”的字符串,它等于“hello world”,

str = "hello world"

它应该以某种方式返回字符串“world”,因为它位于空格之后。我该怎么做?

【问题讨论】:

标签: lua


【解决方案1】:

如果你只想取第一个空格前的第一个值:

local result = str:match("%w+")

print(result)

如果你想收集以空格分隔的每个元素:

local tb = {}

for i in str:gmatch("%w+") do
    print(i) 
    table.insert(tb, i) -- in case you want to store each separate element in a table
end

【讨论】:

    猜你喜欢
    • 2016-08-25
    • 2010-11-28
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    相关资源
    最近更新 更多