I had been experimenting with the for
loop, and sub
method, when an idea occurred in my head, Is it possible for the for loop to detect a certain string within a string? And if so, will it replace the word?
, and, sadly, the code did detect the string within the string, however, it only replaced the first letter of the certain string, and not the whole. I am currently confused on why that is, because, I thought I had coded it right, but, sadly, it did not end up like I wanted it to, here is the code I used in my experiment;
local msg = "I'm a Test! :D"; local chk = ""; local find = "Test"; local Replacement = "string"; for i = 1, msg:len() do if msg:lower():sub(i,i+find:len()-1) ~= find:lower() then chk = chk .. msg:sub(i,i) else chk = msg:sub(i,i+find:len()) .. Replacement print("FOUND MATCH") end wait(.1) print(chk) end print(chk)
You can just use string.find()
local msg = "I'm a Test! :D" local find = "Test" local replacement = "string" local start, fin = string.find(msg, "Test") print(string.sub(msg,1,start -1)..replacement..string.sub(msg,fin + 1))
Can't test this right now, so it might not work and I'll edit later.