StringReplace alternatives to improve performance

前端 未结 8 770
温柔的废话
温柔的废话 2021-02-04 12:46

I am using StringReplace to replace > and < by the char itself in a generated XML like this:

StringReplace(xml.Text,\'>\',\'>\',[rfReplaceA         


        
8条回答
  •  难免孤独
    2021-02-04 13:30

    When you are dealing with a multiline text files, you can get some performance by processing it line by line. This approach reduced time in about 90% to replaces on >1MB xml file.

    procedure ReplaceMultilineString(xml: TStrings);
    var
      i: Integer;
      line: String;
    begin
      for i:=0 to xml.Count-1 do
      begin
        line := xml[i];
        line := StringReplace(line, '>', '>', [rfReplaceAll]);
        line := StringReplace(line, '<', '<', [rfReplaceAll]);
        xml[i] := line;
      end;
    end;
    

提交回复
热议问题