I am using a for do loop and StringReplace... I see the problem, but I don't know how to fix it. I want to substitute an entire string with characters that are saved in an array, then I want to save the COMPLETE substituted string into sLine. The problem is, is that every time the loop(L) repeats the code, the previous substituted value in sLine gets thrown away, so that it can store the new semi-substituted string. (By "semi", I mean it only actually substitute's 1 character in the string at a time.)
Is there any way to substitute the entire string first while keeping all its substituted characters, then save it in sLine?
var
sEncryptInput, sLine : string;
K, L : integer;
begin
redOutEncrypt.Clear;
for K := 0 to redInEncrypt.Lines.Count do
begin
sEncryptInput := redInEncrypt.Lines[K];
for L := 0 to 25 do
begin
sLine := StringReplace(UpperCase(sEncryptInput), UpperCase(arrPlainAlph[L]), arrOffset[L], [rfReplaceAll, rfIgnoreCase]);
end;
redOutEncrypt.Lines.Add(sLine);
end;
end;
CodePudding user response:
The problem is that every iteration of your loop is reading sEncryptInput
as its source to then call StringReplace
. Since sEncryptInput
is not being updated within each iteration your sLine
contains only changes made in last loop iteration.
To keep all changes made in each iteration your loops needs to both read from and write to the same variable. So your code should look like this:
...
sEncryptInput := redInEncrypt.Lines[K];
//Copy unmodified sEncryptInput to sLine before doing any changes in your loop
sLine = sEncryptInput;
for L := 0 to 25 do
...