1

I'm trying to split a string on a space in Lua. What I mean is if i had a string named "str" and it was equal to "hello world",

str = "hello world"

it should somehow return the string "world" because it is after the space. How do I do this?

1 Answers1

2

if you want to take only the first value before the first space:

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

print(result)

if you want to collect each of the elements separated by space:

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
Vinni Marcon
  • 606
  • 1
  • 4
  • 18