首页 > 代码库 > delphi 字符串查找

delphi 字符串查找

1、在源字符串Src中查找子串S,返回Src中S之前的部分

Function Before( Src,S:string ): string ;
Var
  F: Word ;
begin
    if Src = http://www.mamicode.com/‘‘ then
      Before := ‘‘;
    F := Pos(S, Src);
  if F = 0 then
  begin
    Before := S;
  end
  else
    Before := Copy(Src,1,F-1);
end;

 

2、在源字符串Src中查找子串S,返回Src中S之后的部分

function After(Src: string; S: string):string;
Var
  F: Word ;
begin
  if Src = http://www.mamicode.com/‘‘ then After := ‘‘;
  F := Pos(S, Src);
  if F = 0 then
    After := ‘‘
  else
    After := Copy(Src, F+Length(S), Length(Src));
end;

 

 

3、返回Src中 子串first与second之间的部分,找到立即返回

function Between(Src: string; first, second: string):string;
begin
  Between := Before(After(Src, first), second);
end;