How to find a specific string in a bigger string, but ONLY that string?

I’m making a console for executing commands, and this is the part that checks if the correct string is there:

for i, v in rc do
		if not table.find(rc, string.match(cmd, v)) then game.ReplicatedStorage.Remotes.Console.MainEvent:FireClient(plr, true) sentCommand = nil else sentCommand = string.match(cmd, v) game.ReplicatedStorage.Remotes.Console.MainEvent:FireClient(plr, false) end
		print(i)
	end

It gets the values plr, and cmd from a remote event, the cmd value contains the full command as a string.
There is a table called “rc” which contains all the available commands. (rc is registered commands).

I only want it to detect if the string, like “exec” is at the beginning of the string, and if it doesnt contain any random words, like “armexec”. I want it to only detect if that only word is at the beginning of the string and it is the only word, with no additional things.

Might be confusing, but thats what im aiming for.

PS: Heres the “rc” table.

local rc = {
	"setrolls";
	"kickusr";
	"shutdownsv";
	"exec";
}
2 Likes

By the way, the print(i) was just debug. Its not there anymore in the real code.

2 Likes

i believe there is option to split string into table, and then read from this table, use string.split(string," ") to do that

EDIT: from this point you can check if specific strings match, for example cmd[1] is player or cmd[2] command, it’s up to you

1 Like

The following function returns true/false if it finds the small string inside the big one

function has_string(big_string, small_string)
	local pattern = small_string
	local range = string.find(big_string, pattern) 
	return range and true or false
end

I tested it with your case like this:

local rc = {
	"setrolls";
	"kickusr";
	"shutdownsv";
	"exec";
}

for _, str in pairs(rc) do
	print(has_string("Lorem ipsum dolor sit amet, shutdownsv consectetur adipiscing elit. Nullam eget.", str))
end

The result:
image

1 Like

I meant it like, if the user types for example “exec (parameters)”, it’ll execute, but if they type like “aexec (parameters)” it wont work. And also if its like “hello exec (parameters)” it also shouldnt work. It should only work if the string is at the beginning, and is the only word at the beginning.

I meant it like, if the user types for example “exec (parameters)”, it’ll execute, but if they type like “aexec (parameters)” it wont work. And also if its like “hello exec (parameters)” it also shouldnt work. It should only work if the string is at the beginning, and is the only word at the beginning.

its fine if its not possible, i was just curious

Nevermind, i just used string.split to split it into parts then check if the 1st argument is equal to the correct argument

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