Reputation: 137
I have the lines in memo like:
111111.kll
222222.kll
I need to remove last 4 char from each line to obtain result like:
111111
222222
Upvotes: 0
Views: 2801
Reputation: 196
Well, there is a specif function to replace characters. If you want to remove ".kll" for example there is no need to use a Loop.
Memo1.Lines.Text := StringReplace(Memo1.Lines.Text,'.kll','',[rfReplaceAll, rfIgnoreCase]);
Hope it helps!
Upvotes: 2
Reputation: 596652
As an alternative to GolezTrol's solution, you can manipulate the Memo content directly, instead of making a copy of it in memory first:
var
i, LineStart, LineLen: Integer;
begin
Memo1.Lines.BeginUpdate;
try
for i := 0 to Memo1.Lines.Count - 1 do
begin
LineStart := Memo1.Perform(EM_LINEINDEX, i, 0);
LineLen := Memo1.Perform(EM_LINELENGTH, LineStart, 0);
Memo1.Perform(EM_SETSEL, LineStart + LineLen - 4, LineStart + LineLen);
Memo1.SelText := '';
end;
finally
Memo1.Lines.EndUpdate;
end;
end;
Upvotes: 2
Reputation: 116110
With a for
loop, you can iterate over the lines. Using copy
, you can get a part of a line and assign it back to the line:
for i := 0 to Memo1.Lines.Count - 1 do
Memo1.Lines[i] := Copy(Memo1.Lines[i], 1, Length(Memo1.Lines[i]) - 4);
Now, changing the lines of a memo isn't very fast, so if you have many lines, you may want to use a stringlist instead. You can process all the lines in the stringlist, and only put them back in the memo after you're done. That way, the contents of the memo change only one time:
var
i: Integer;
sl: TStringList;
begin
sl := TStringList.Create;
try
sl.Text := Memo1.Text;
for i := 0 to sl.Count - 1 do
sl[i] := Copy(sl[i], 1, Length(sl[i]) - 4);
Memo1.Text := sl.Text;
finally
sl.Free;
end;
end;
Upvotes: 2