编辑:看起来我错了 – UniqueString不是问题.实际的瓶颈似乎是按字符访问字符串.鉴于我的整个答案都是无关紧要的,我完全取而代之.
如果您使用PChar避免重新计算字符串偏移量,同时仍然通过Txt [Idx]更新字符串,则该方法要快得多(在我的1000次运行测试中为5秒至0.5秒).
这是我的版本:
procedure TForm1.btn1Click(Sender: TObject);
var
Idx: Integer;
Tag: Boolean;
p : PChar;
Txt : string;
begin
Tag := False;
Txt := Mem1.Text;
p := PChar(txt);
Dec(p);
For Idx := 0 to Length(Txt) - 1 Do
Begin
Inc(p);
If (not Tag and (p^ = '
Tag := True;
Continue;
end
Else If (Tag and (p^ = '>')) Then
Begin
Tag := False;
Continue;
end;
If Tag Then Continue;
If (not (p^ in [#10, #13, #32])) Then begin
Txt[Idx] := '0';
end;
end;
mem2.Text := Txt;
end;