Delphi获取两个分割字符串之间的字符
最近研究微信公众号音频下载,通过接口获取文章数据后,需要解析其中的关键内容,我发现会大量用到获取某两个字符串之间的字符串这个功能,所以写了如下函数。
Function GetBetweenStr(S: String; Delimite1, Delimite2: String; St: Tstrings): Boolean;
Var
P1, P2, L: integer;
Begin
P1 := Posex(Delimite1, S, 1);
P2:= Posex(Delimite2, S, P1);
While (P1 > 0) And (P2 > P1) Do
Begin
Application.ProcessMessages;
L := P2 - P1 + Length(Delimite2);
St.Add(Copy(S, P1, L));
S := Copy(S, P2 + Length(Delimite2), Maxint);
P1 := Posex(Delimite1, S, 1);
P2:= Posex(Delimite2, S, P1);
End;
Result := St.Count > 0;
End;
用法:
Var St:Tstringlist;
Begin
St:=Tstringlist.Create;
IF GetBetweenStr(Htmlstr, '<section><mpvoice class', '</section>', St) THEN
ShowMessage(St.text);
St.Free;
End;

浙公网安备 33010602011771号