Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Splitting a string into arguments?

Asked by 5 years ago

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?

0
Is there a reason as to why you need to do this. User#24403 69 — 5y
0
I believe it is to change who is being killed. TimeOverseer18 1 — 5y

1 answer

Log in to vote
2
Answered by
clc02 553 Moderation Voter
5 years ago

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

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. Ziffixture 6913 — 5y
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 . clc02 553 — 5y
0
It works with string literals as well, only you need to wrap in parenthesis ("string"):gmatch(pattern) User#24403 69 — 5y
0
Thank you soo much! OptimisticSide 199 — 5y
0
Learned something new I guess clc02 553 — 5y
Ad

Answer this question