Answered by
8 years ago Edited 8 years ago
First, if you use string methods your code becomes a little shorter and nicer. You can also use -1
to mean "end of string" so you don't have to say #message
:
1 | message:sub( message:find( "%s" ), - 1 ) |
But instead, you can use match. You describe a pattern and put ()
around the pieces you care about.
For example, you can capture everything from the beginning until the first space(s), then everything after the first space(s):
6 | local before, after = message:match( "^(%S+)%s+(.+)$" ) |
Note that before
and after
will be nil
if the message isn't the right form.
This would break the example into
"Hello dude."
--> "Hello"
and "dude."
"/kill all"
--> "/kill"
"all"
"I hate hate."
--> "I"
"hate hate."