2

<!--HTMLから読み取り可能なテキストを文字列に抽出しているので、と-->タグの間の既存のテキストを削除する必要があります。これを達成するための最も効率的な方法は何でしょうか?

今、私はそれをこのようにやっています:

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;

これを行うためのより良い方法はありますか?

4

1 に答える 1

5

次のようなものを試してください。

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; 
于 2012-07-18T22:00:50.073 に答える