删除Delphi中所有标签之间的变量子串的实例 [英] Delete all instance of variable substring between tags in Delphi

查看:125
本文介绍了删除Delphi中所有标签之间的变量子串的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从HTML中将可读取的文本解压缩成字符串,我需要删除<! - 之间的现有文本。 - > 标签。实现这一点最有效的方法是什么?



现在我这样做:

  function RemoveIEScripts(const s:string):string; 
var
i:整数;
InTag:Boolean;
begin
结果:='';
InTag:= False;
for i:= 1 to Length(s)-3 do
begin
if(s [i] ='<')和(s [i + 1] ='! )和(s [i + 2] =' - ')然后
inTag:= True
else if(s [i] =' - ')和(s [i + 1] =' ')和(s [i + 2] ='>')然后
inTag:= False
如果不是InTag,则
结果:= Result + s [i];
结束
结束

有没有更好的方法?

解决方案

尝试这样的东西:

  function Removeiescripts(const s:string) :字符串
var
I,J:整数;
begin
结果:= s;
I:= 1;
repeat
I:= PosEx('<! - ',Result,I);
if I = 0 then Break;
J:= PosEx(' - >',Result,I + 4); // 4 = Length('<! - ')
如果J = 0,则Break;
删除(Result,I,(J + 3)-I); // 3 = Length(' - >')
until False;
结束


I'm extracting readable text from HTML into a string and I need to remove the existing text between the <!-- and the --> tags. What would be the most efficient way to achieve this?

Right now I'm doing it this way:

function RemoveIEScripts(const s: string): string;
var
  i: Integer;
  InTag: Boolean;
begin
   Result := '';
   InTag := False;
   for i := 1 to Length(s)-3 do
   begin
      if (s[i] = '<') and (s[i+1] = '!') and (s[i+2] = '-') then
         inTag := True
      else if (s[i] = '-') and (s[i+1] = '-') and (s[i+2] = '>') then
             inTag := False
           else if not InTag then
      Result := Result + s[i];
   end;
end;

Is there a better way to do this?

解决方案

Try something like this:

function RemoveIEScripts(const s: string): string; 
var 
  I, J: Integer; 
begin 
  Result := s; 
  I := 1;
  repeat
    I := PosEx('<!--', Result, I);
    if I = 0 then Break;
    J := PosEx('-->', Result, I+4); // 4 = Length('<!--')
    if J = 0 then Break;
    Delete(Result, I, (J+3)-I); // 3 = Length('-->')
  until False;
end; 

这篇关于删除Delphi中所有标签之间的变量子串的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆