How do I get the third word from a string?

So, I want to do a command, but
msg:sub(3)

and it dosent work.

So how do I get the third word from a message?

Thanks.

To separate a string into tokens you can do something like this:

local msg = "hello world, i'm a string!"
local tokens = {}
for word in msg:gmatch("%S+") do
	table.insert(tokens, word)
end

The third word in the string would be the third item in the resulting array.

print(tokens[3]) -- i'm

What does %S+ mean? Just wondering.

It’s a string pattern. In this case it tells the gmatch function to select all substrings that do not contain whitespace characters.

More info:

So, I tried to re-write your script, and this is what I got:

DEVFORUMPIC1

(It printed nil)

and yours,

DEVFORUMPIC2

printed i’m

The pattern is %S+, not $S+.

1 Like