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?
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?