Delphi在字符串列表之间提取文本

时间:2017-07-15 13:33:57

标签: delphi extract

我需要从字符串列表中提取文本吗?这非常有效,所以这只从字符串中提取。我需要从Tmemo的列表中提取。

由于

function ExtractTextBetween(const Input, Delim1, Delim2: string): string;
var
  aPos, bPos: Integer;
begin
  result := '';
  aPos := Pos(Delim1, Input);
  if aPos > 0 then begin
    bPos := PosEx(Delim2, Input, aPos + Length(Delim1));
    if bPos > 0 then begin
      result := Copy(Input, aPos + Length(Delim1), bPos - (aPos + Length(Delim1)));
    end;
  end;
end;

1 个答案:

答案 0 :(得分:-1)

不明白为什么不正确分隔,我只需要提取电子邮件

示例字符串

"电子邮件":" xxxx@xxxx.xom","帽":" 00000",

memo3.Lines.AddStrings(ExtractText(memo2.Text,',"email":"','",')) ;

<强>结果 xxxx@xxxx.xom",&#34;帽

function ExtractText(const Str: string; const Delim1, Delim2: string): TStringList;
var
  c,pos1, pos2: integer;
begin
  result:=TStringList.Create;
  c:=1;
  pos1:=1;

  while pos1>0 do
  begin
    pos1 := PosEx(Delim1, Str,c);
    if pos1 > 0 then begin
      pos2 := PosEx(Delim2, Str, pos1+1);
    if pos2 > 0 then
      result.Add(Copy(Str, pos1 + length(delim1), pos2 - (length(delim2) + pos1)));
      c:=pos1+1;
     end;

  end;