【问题标题】:(LUA) is there any way for me to cut a message and save it inside variables?(LUA)我有什么办法可以剪切消息并将其保存在变量中?
【发布时间】:2021-03-07 02:39:57
【问题描述】:

我可以使用 string.gsub(message, " ") 但它只会删减单词。

我在http://lua-users.org/wiki/StringLibraryTutorial 上进行了搜索,但在那里我找不到任何解决方案

如何将这些单词保存到变量中? 例如我有 message = "fun 1 true enjoy"

我希望变量有

var level = 1
var good = true
var message = "enjoy"

【问题讨论】:

  • 哪个 Lua 版本?使用 Lua 5.3.5 进行迭代非常简单:for word in message:gmatch('%w+') do print('Do here what you want with:',word) end

标签: lua lua-patterns


【解决方案1】:

使用string.match提取字段,然后将它们转换为合适的类型:

message =  "fun 1 true enjoy"
level,good,message = message:match("%S+%s+(%S+)%s+(%S+)%s+(%S+)")
level = tonumber(level)
 good = good=="true"
print(level,good,message)
print(type(level),type(good),type(message))

match 中的模式跳过第一个字段并捕获以下三个字段;字段由空格分隔。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-14
    • 2011-09-22
    • 2016-05-18
    • 2010-11-15
    • 2019-09-20
    • 2022-06-22
    • 1970-01-01
    • 2015-02-25
    相关资源
    最近更新 更多