
procedure TForm1.btn1Click(Sender: TObject);var Txt: String; IDx: Integer; Tag: Boolean;begin Tag := False; Txt := mem1.Text; For IDx := 0 to Length(Txt) - 1 Do Begin If (Txt[IDx] = '<') Then Tag := True Else If (Txt[IDx] = '>') Then Begin Tag := False; Continue; end; If Tag Then Continue; If (not (Txt[IDx] in [#10,#13,#32])) Then Txt[IDx] := '0'; end; mem2.Text := Txt;end;
HTML文本永远不会有“<”或“>”外部标签(在文本中间),所以我不需要担心这个.
谢谢!
解决方法 编辑:看起来我错了 – 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^ = '<')) Then begin Tag := True; Continue; end Else If (Tag and (p^ = '>')) Then Begin Tag := False; Continue; end; If Tag Then Continue; If (not (p^ in [#10,#32])) Then begin Txt[IDx] := '0'; end; end; mem2.Text := Txt;end;总结
以上是内存溢出为你收集整理的替换HTML字符串中的字符 – 除标记外全部内容,希望文章能够帮你解决替换HTML字符串中的字符 – 除标记外所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)