How do I make a command with substring?

Asked

Viewed 58 times

1

How do I substring with a command (ultilizing io.read)? example:

io.read() question I say, Say Hello World then the console "printa" "Hello World".

"Say" is the command "Hello World" is the substring.

1 answer

1

Use string.match as in the example below:

s=io.read()
c,m=s:match("(%S+)%s+(.+)$")
if c=="say" then
    print(m)
end

The default used in match is: search and capture a string of characters not spaces, skip spaces, capture the rest of the line.

  • Thank you for the answer. Just one more thing, you can comment on the first two lines so I know what each thing does?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.