How to find exact word in a string

Okay, so, let me cut straight to this:

I’m trying to develop a search system, to put it simply. Everything’s working amazing, just, I’m having troubles with string:match() / string:find() (my problem is not trying to find the difference between the two, btw). The problem I’m having is I want it to only activate the function if it has the exact word in it. Since, let’s say I had “Hello” as my string, using string:match() (or find, again, got no idea on the difference but I could easily find it online with a search or two haha), it would execute the function if you even just typed in “h”.

I’m sure I made this sound stupidly complicated, so here’s the gist:
string:match() works perfectly, but I want it to only activate the function I have it in if it has the exact word I want.

Sorry if I made this sound really complicated or messed up any terms, compared to most people, I’m not very good.

3 Likes
local message = "Hello"
print(message:find("Hello"))
-- Returns 1-5 (the range of the find).
print(message:find("Hell"))
-- Returns undefined

Just do if message:find(...) then

5 Likes

Thank you for this! Testing it out right now, I’ll let you know if it works. I honestly should’ve thought of this, it seems ridiculously obvious haha.

Might I also add how hilariously quick responses come on the dev forum, it’s hilarious to me! Thank you for this, again!

2 Likes

Just a question, I’m struggling to make this work for any string. I was hoping to use string.sub() to check if the string contained the letters other than the very last. I’m struggling to do this myself and it’s being incredibly glitchy for me. Could you provide an example of this using the scenario used above?

What exatly is it you’re trying to do with string.sub()?

if string.find(text,“somethinghere”) then
string.gsub(text,“somethinghere”,“replaceto”)
end

Thanks! Sorry but could you explain how this works so I gain a better understanding of it?

this site should be better.

Returns the substring of s that starts at i and continues until j; i and j can be negative. If j is absent, then it is assumed to be equal to -1 (which is the same as the string length).

The definition isn’t very helpful to people who don’t understand it - the unhelpful variable names also help their confusion.

Sorry for asking but, what exactly do you mean by “somethinghere”, “text” and “replaceto”?

the text is example a text called
local text = “blöabalbalbasomethingherebablablabllaba”
if string.find(text,“somethinghere”) then
text = text string.gsub(text,“somethinghere”,“replaceto”)
end
print(text)

– second example u could just use
local text = “blöabalbalbasomethingherebablablabllaba”
text = text string.gsub(text,“somethinghere”,“replaceto”)

print(text) – to find out what this does just run it in a Roblox script and check output console.

Nothing happens with either of these, they both just display “blöabalbalbasomethingherebablablabllaba” to the output

maybe bc I am using german key called “ö”

but should do this :
local text = “blabalbalbasomethingherebablablabllaba”
text = text string.gsub(text,“somethinghere”,“ replaceto ”)
print(text) – blabalbalba replaceto bablablabllaba

Ohhh!

This really helped clear it up. It makes a lot more sense now. So in the case of “Hello Robloxians!” I would do

local text = “Hello Robloxians!”
text = text string.gsub(text,“Robloxians!”,“People!”)
print(text) – Hello People!

That cleared everything up a lot, thank you!

Edit: Do you think you could write this using this example?:

So, it looks for Hello, if it finds hello, then it looks for all letters below it. Except, I want the message to be able to be anything. So I’d like it to be able to work with “Roblox” for example. So basically, how would you make the above work if the message were anything? Hope this made sense.

Just change the “Hello” at the message = part to set the message

and at the message:find(“Hello”) part inside the “Hello” remove hello and enter the word you want to look for.

I think I may have found another way to do this. I’ll give that a go as well but do you know how you would find the first half of any string? So for example: “Hi” would find the first half of the word, which would be “H”. Then for “Roblox” the first half would be “Rob”

message:sub(1, math.modf(#message / 2))

I havent tried the code above but it should work.

Hmm, it works but it doesn’t work. It finds the first half of the word perfectly, but when you type in the first half of the word, it doesn’t execute the function I want it to. Here’s my code:

local searchbar = script.Parent
function UpdateInputOfSearchText()
	local InputText=string.upper(searchbar.Text)
	for _,button in pairs(script.Parent.Parent.Parent.Searches:GetChildren())do
		if button:IsA("IntValue")then
			if InputText==""or string.upper(button.Name):find(InputText) then
				local sub = button.Name:sub(1, math.modf(#button.Name / 2))
				print(string.upper(sub))
				if button.Name == string.upper(InputText):find(string.upper(sub)) then
					script.Parent.BackgroundColor3 = Color3.fromRGB(195, 0, 0)
				else
					script.Parent.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
				end
			else
				script.Parent.BackgroundColor3 = Color3.fromRGB(195, 0, 0)
			end
		end
	end
end
searchbar.Changed:Connect(UpdateInputOfSearchText)

Basically, I have one of my strings as “Tree”, when I type in “tr” it should make the text go red, but it just stays white, nothing goes in the output, either.

U should use Script.Parent.TextColor …