Search code examples
lualua-patterns

(LUA) is there any way for me to cut a message and save it inside variables?


i can use string.gsub(message, " ") but it only cuts the words.

i searched on http://lua-users.org/wiki/StringLibraryTutorial but i cant find any solution for this there

how can i save these words into variables? for example i have message = "fun 1 true enjoy"

and i want variables to have

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

Solution

  • Use string.match to extract the fields and then convert them to suitable types:

    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))
    

    The pattern in match skips the first field and captures the following three fields; fields are separated by whitespace.