Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How come the 'for' loop doesn't detect a certain string in the code I am current using?

Asked by 9 years ago

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)

1 answer

Log in to vote
0
Answered by 9 years ago

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.

0
I tried using the 'find' method as well, but, sadly, that did not work either. TheeDeathCaster 2368 — 9y
0
It should be pretty simple, I'll edit my answer later to show you how it's done aquathorn321 858 — 9y
Ad

Answer this question