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 1 year ago

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

-- context
local Message = ".lock true"
local SL_SETTINGS = {CommandPrefix = "."}

-- actual script
local splitMessage = Message:split(" ")
local command = splitMessage[1]
local no_prefix = string.gsub(command, SL_SETTINGS.CommandPrefix, "")

print(command, no_prefix)
--> ".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 1 year ago
Edited 1 year 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.

local Message = ".lock true"
local SL_SETTINGS = {CommandPrefix = "."}

local splitMessage = Message:split(" ")
local command = splitMessage[1]
local no_prefix = command:gsub("%p", "") -- or command:split(SL_SETTINGS.CommandPrefix)[2]

print(command, no_prefix)
--> ".lock" "lock"
0
oh my god, yes. i completely forgot about literals. thank you! loowa_yawn 383 — 1y
Ad

Answer this question