Return an string where all or the first occurrence(s) of the given string, is replaced with something else

What I want to achieve is make an function which returns an copy of the given string, where the first or all occurrence(s) of other string, are replaced with other string/value.

Yes - string.gsub does exist, but it uses string patterns, which is not what I want. And so, what I want to achieve is basically string.gsub but without using string patterns.

My current code is the following:

local function GSub2(String, StrMatch)
	local function LoopString(Str, MatchStr)
		local CurrIdx = 0

		local function Iterator()
			CurrIdx += 1
			local Start, End = Str:find(MatchStr, CurrIdx, true)

			if Start and End then
				return Start and End
			end
		end

		return Iterator
	end
	
	for Start, End in LoopString(String, StrMatch) do
		local Str = String:sub(Start, End)
		
		
	end
end

Although, my main issue is discovering how I’ll return a copy of the given string without the match? Basically, how I’ll remove it from it?

Edit: I’ll also have to fix this code a bit.

You don’t have to use string patterns.

print(string.gsub("Hello", "a", "e")) -- Hallo 1

That won’t do what you expect, return Start, End so you return both.

Yes, it doesn’t need to, but if it does match an magic character or an character class, it’ll achieve a result I don’t want.

Also, thank you for pointing that out. That was also the error which needed a ‘fix’.

You can escape magic characters by prepending a % sign before it

Let me elaborate, since I did not before:

The real reason behind this all, is to make some sort of converter which adds the % sign before all magic characters found. Or an copy of string.gsub, which does the same but without using string patterns.

Now, back to the topic, how would I do what’s asked in the post?