How to escape magic characters?

Basically, I want it to print out “test+” but in the output I get “1 5”.
How do I escape the whole string without manually putting “%” before the “+” ?
Any advice?
Code:

local test1 = "test+"
local test2 = "tEsT+"
if (string.find(string.lower(test2),string.lower(test1),1,true)) ~= nil then
print(string.find(string.lower(test2),string.lower(test1),1,true))
end
2 Likes

Its printing “1 5” because you’re using “string.find()”

can you explain more on what you want to achieve?

i want to input ANY type of text into a field
lets say “TEST++”

the issue is i need a way to escape the ‘+’ without manually putting ‘%’ before it.
basically im looking for a string function or something that does that.

Are there any other characters you want to escape? Why do you want to escape the plus character in specific?

what is the code being used for? I mean why do you have to do it like that, what do you need it for?

im just trying to learn string functions and stuff, theres no reason why i would specifcally choose “++” I could also be choosing “-” or “.” etc etc.

Im just trying to improve my knowledge here thats all, this stuff is used in HD admin for example and its just good for me to know, these “magic characters” are whats bothering me.

I writed some code for you.

local Text = "your text" -- the text
local letter = "your letter" -- The letter that you want to check 
if string.find(Text, letter) ~= nil then
    print("Found something") 
    -- do some code here if something is detected
else 
    print("there is nothing") 
    -- do some code here if nothing is detected
end

Edit : *Grammar correction

Here’s a basic function that escapes magic characters:

local MAGIC_CHARS = {'$', '%', '^', '*', '(', ')', '.', '[', ']', '+', '-', '?'}
local function ignoreMagicCharacters(str: string): string
	
	local cStr = ""
	local char
	for i = 1, string.len(str) do
		char = string.sub(str, i, i)
		if table.find(MAGIC_CHARS, char) then
			cStr ..= "%" .. char
		else
			cStr ..= char
		end
	end
	return cStr
end


print(ignoreMagicCharacters("test++")) --// test%+%+
print(ignoreMagicCharacters('something%$')) --// something%%%$
1 Like