I’ve created a framework to operate in my games which handles everything in there, there are also chat commands and I’ve run into a bit of difficulty.
An example command would be “-allhands 10 bla bla bla”
or “-ah 10 bla bla bla”
I’m unsure how to remove the “-allhands 10” or “-ah 10” and only leaving the “bla bla bla” from the string before sending it through the rest of the system, what I need is an efficient way to remove certain words from strings preferably in a function.
An example of what I’d like to achieve overall is like this:
COMMAND: “-command 25 hello there”
local Result = RemoveWords(COMMAND, 1)
print(Result) > “25 hello there”
or:
COMMAND: "-test first second
local Result = RemoveWords(COMMAND, 3)
print(Result) > “-test first”
Hopefully I’ve explained it easily enough, thanks in advance for any assistance!
you can use string.split(str,seperator).
for example, string.split("-ah 10 bla bla bla"," ")
returns the table {"-ah","10","bla","bla","bla"}, which you can then loop through and do whatever yk
local x = "The quick brown fox jumps over the lazy dog."
function removeFirst2Words(str: string)
local split = str:split(" ")
table.remove(split,1)
table.remove(split,2)
return table.concat(split, " ")
end
print(removeFirst2Words(x))
local x = "The quick brown fox jumps over the lazy dog."
function removeFirst2Words(str: string)
return table.concat(string.split(str," "), " ",3)
end
print(removeFirst2Words(x)) -- brown fox jumps over the lazy dog.
With your help, I’ve come up with a function that works as needed!
Thank you very much.
function Tools.RemoveWordsFromStart(str, count)
if str and count then
local StoredString = str
for i = 1, count do
if StoredString then
StoredString = RemoveStartWord(StoredString)
end
end
return StoredString
else
return nil
end
end
function RemoveStartWord(str)
local ReturnString = str:match(".-%s(.+)")
return ReturnString
end