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

string substitution returning unwanted value?

Asked by 2 years ago

i am working on command formatting right now, and for my code i have implemented this solution:

01-- context
02local Message = ".lock true"
03local SL_SETTINGS = {CommandPrefix = "."}
04 
05-- actual script
06local splitMessage = Message:split(" ")
07local command = splitMessage[1]
08local no_prefix = string.gsub(command, SL_SETTINGS.CommandPrefix, "")
09 
10print(command, no_prefix)
11--> ".lock" ""

the issue i am experiencing is that line 8 is producing a substituted string that i don't want, i get an empty string when i expect "lock"

does anybody know why this is happening and how i could fix this code?

1 answer

Log in to vote
2
Answered by 2 years ago
Edited 2 years ago

. is a string pattern that represents any character; therefore, it is replacing all the characters in the string.

You can use the %p string pattern, which removes all punctuation from the string, or you can split the prefix from the string.

1local Message = ".lock true"
2local SL_SETTINGS = {CommandPrefix = "."}
3 
4local splitMessage = Message:split(" ")
5local command = splitMessage[1]
6local no_prefix = command:gsub("%p", "") -- or command:split(SL_SETTINGS.CommandPrefix)[2]
7 
8print(command, no_prefix)
9--> ".lock" "lock"
0
oh my god, yes. i completely forgot about literals. thank you! loowa_yawn 383 — 2y
Ad

Answer this question