Issues with <String>:find(...)

Hello everyone!

I’m currently working on a module to make uses of RichText easier. Since a few minutes, I’m struggling trying to find a fix for the issue I’m currently having. For some reasons, string:find() doesn’t seem to find an element in a string. Here’s what I mean:

My string is “This is a test, !COLOR-END!”.
My code is string:find("!COLOR-END!")

It is clear that “!COLOR-END!” is in the string, however my code returns nil and I don’t understand why.

Any help is greatly appreciated!

do string.find instead of string:find | string:find would be accessing a member function of an object while string.find would be accessing a function in the string class.

I tested your suggestion however it still doesn’t seems to work.
image

Put a % before the -. - is a reserved character in lua string patterns

1 Like

Try doing it without the !'s at the beginning/end of the string? My guess is that ! is being treated as a special character.

If the above suggestion works, try doing this:

local str = "This is a test, !COLOR-END!"
local found = str:find("\!COLOR-END\!")

local stringssss = “This is a test, !COLOR-END!”
print(string.find ( stringssss, “!COLOR-END!”, 1, true)) | Setting the last parameter to string.find to true makes the function not detect magic characters which the hyphen (-) is reserved as | You can read more about the string.find function in the API reference here string | Roblox Creator Documentation

- is a magic character, more information about them can be found here. Magic characters can be ignored by placing a “%” symbol infront of them.

string.find(yourText, "!COLOR%-END!")
yourText:find("!COLOR%-END!")

PS: magic characters can be ignored completely, by setting the fourth string.find parameter to true(for intended behavior the third parameter should be set to 1):

string.find(yourText, "!COLOR-END!", 1, true)
yourText:find("!COLOR-END!", 1, true)
3 Likes