I'd like to remove the first 2 words in a string

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!

3 Likes

What I would do is look for the second space in the string with string.find()

you can use the init parameter to search after the first space

2 Likes

You can use string.gsub() to manipulate the string.
Example:
-command give apple me
local Result = string.gsub(Sentence, ‘command’, ‘’)
print(Result)

Output:
give apple me

2 Likes

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

1 Like

This removes first 2 characters in a string

local str="hello"
print(string.sub(str,3,#str)) --llo
2 Likes

thats the first two characters, not words :upside_down_face:

2 Likes
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))

surely there is a more efficient way

1 Like
local str = "the lazy fox jumps over the river or something"
str = str:match(".-%s.-%s(.+)")
print(str) -- fox jumps over the river or something

Explanation:

This use’s the lua string captures

.- captures anything not greedy (meaning it will try to capture the minimal amount of characters
%s tries to capture a string

By combining ".-%s" we try to capture all the characters to capture all the characters to a space. So doing that twice will capture the first 2 words.

(.+) The parenthesis is a capture group meaning that it will return whatever’s inside of it. .+ means we try to capture anything (greedy)

3 Likes

Yes.

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.
2 Likes

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
2 Likes

I’m able to use this in other functions, thanks so much for the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.