Delphi 删除字符串中的重复文本

Delphi 删除字符串中的重复文本,delphi,delphi-xe8,Delphi,Delphi Xe8,我有一个字符串,它有一些像这样的文本 str := 'Hi there My name is Vlark and this is my images <img src=""><img src=""> But This images need to be controlled <img src=""><img src=""><img src=""><img src="">'; str:=“你好,我的名字是Vlark,这是我

我有一个字符串,它有一些像这样的文本

str := 'Hi there My name is Vlark and this is my images <img src=""><img src=""> But This images need to be controlled <img src=""><img src=""><img src=""><img src="">'; 
str:=“你好,我的名字是Vlark,这是我的图片,但这些图片需要控制”;
此字符串有6个图像标签
策略:

  • 查找完整封闭标签的位置和长度:
  • 如果计数大于3,则移除标签

函数RemoveExcessiveTags(const s:String):String;
变量
标签,cP,p:整数;
开始
标签:=0;
cP:=1;
结果:=s;
重复
cP:=位置(“”,结果,cP+4);
如果(p>0),则开始
公司(标签);
如果(标记>3),则开始//如果超过3个标记,则删除标记
删除(结果,cP,p-cP+1);
结束
其他的
cP:=p+1;//下一个搜索开始位置
结束
其他的
cP:=0;//我们到达字符串末尾,中止搜索
结束;
直到(cP=0);
结束;
策略:

  • 查找完整封闭标签的位置和长度:
  • 如果计数大于3,则移除标签

函数RemoveExcessiveTags(const s:String):String;
变量
标签,cP,p:整数;
开始
标签:=0;
cP:=1;
结果:=s;
重复
cP:=位置(“”,结果,cP+4);
如果(p>0),则开始
公司(标签);
如果(标记>3),则开始//如果超过3个标记,则删除标记
删除(结果,cP,p-cP+1);
结束
其他的
cP:=p+1;//下一个搜索开始位置
结束
其他的
cP:=0;//我们到达字符串末尾,中止搜索
结束;
直到(cP=0);
结束;
function RemoveExcessiveTags( const s: String): String;
var
  tags,cP,p : Integer;
begin
  tags := 0;
  cP := 1;
  Result := s;
  repeat
    cP := Pos('<img',Result,cP);
    if (cP > 0) then begin
     // Find end of tag
      p := Pos('>',Result,cP+4);
      if (p > 0) then begin
        Inc(tags);
        if (tags > 3) then begin // Delete tag if more than 3 tags
          Delete(Result,cP,p-cP+1);
        end
        else
          cP := p+1;  // Next search start position
      end
      else
        cP := 0;  // We reached end of string, abort search
    end;
  until (cP = 0);
end;