Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Splitting a string into arguments?

I was working on some chat commands for my game, when I wondered: Is there a way to do something like this?

local string = ";kill me"

To get the output:

Args = {"kill", "me"}

I have heard of using string.gmatch, but I am not quite familiar with gmatch. Any help?

2 comments

0

Is there a reason as to why you need to do this.

0

I believe it is to change who is being killed.

1 answer

✓ Accepted answer

gmatch kinda sorta works like pairs, in that you need to use in when you iterate through it.

for word in string.gmatch("Hello2!,- Lua user", "[^%s]+") do
 print(word)
end

gmatch will pass back to word the snipped bits of a string (The first arguement) that's specified by the pattern (The second arguement) In this case, the pattern is, %s which is all spaces, [^%s] which means anything that is NOT a white space, [^%s]+ which means anything that is NOT a white space, until another match is found.

For long term success read up on the library here h e r e ranOutOfLetters and here

5 comments

0

Decent answer, thouh you could've simplified it to his question by 'for word in String:gmatch("[^%s]+") do', created a table aswell and insert the traversed word for reference. Please make you links understandable too by giving the brief term of what you're linking instead of writing random words.

0

String: is just passing string as the first argument as self, will work for most methods.  It's not a request site, I explained how to do it with what he asking about.  All the links are to places to read up on the string library and patterns, they're really different enough to warrant separating them, just alternate sources if one makes more sense than others .

0

It works with string literals as well, only you need to wrap in parenthesis ("string"):gmatch(pattern)

0

Thank you soo much!

0

Learned something new I guess

This discussion is preserved as it was. New answers, comments, and votes are closed.