频道分类

Delphi 提取标签之间的字符串

作者:admin 来源: 日期:2020/6/25 9:09:19 人气: 标签:

 
Function sExtractBetweenTagsB(Const s, LastTag, FirstTag: string): string;
var
  pLast,pFirst,pNextFirst : Integer;
begin
  pFirst := Pos(FirstTag,s);
  pLast := Pos(LastTag,s);
  while (pLast > 0) and (pFirst > 0) do begin
    if (pFirst > pLast) then // Find next LastTag
      pLast := PosEx(LastTag,s,pLast+Length(LastTag))
    else
    begin
      pNextFirst := PosEx(FirstTag,s,pFirst+Length(FirstTag));
      if (pNextFirst = 0) or (pNextFirst > pLast) then begin
        Result := Copy(s,pFirst,pLast-pFirst+Length(LastTag));
        Exit;
      end
      else
        pFirst := pNextFirst;
    end;
  end;
  Result := '';
end;

var
  s: String;

begin
  s := 'Delphi App Hello Hello SomeText here Hello Hello Hello This is a Test! Hello';
  WriteLn(sExtractBetweenTagsB(s,'Test','Hello'));
end.

输出:
Hello This is a Test

来源:https://stackoverflow.com/questions/30827180/delphi-extract-string-between-tags-duplicate-tags

上一篇:delphi 判断一个颜色是否是亮色下一篇:没有资料