delphi 字符串查找函数  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 字符串查找函数


function getLocation(substr,s:string):integer;
var i,len,x:integer;
    tmp:string;
begin
  Result:=0;
  len:=length(substr);
  if len=0 then exit;
  tmp:=s;
  x:=0;
  while length(tmp)>0 do
  begin
    i:=pos(substr,tmp);
    if i>0 then
    begin
      if (i=1) then
      begin
        if not(tmp[len+1] in['a'..'z','A'..'Z']) then
        begin
          Result:=1;
          exit;
        end;
      end
      else if (not(tmp[i-1] in['a'..'z','A'..'Z']))and(not(tmp[i+len] in['a'..'z','A'..'Z'])) then
      begin
        Result:=x+i;
        exit;
      end
      else
      begin
        x:=x+i+len-1;
        tmp:=copy(tmp,i+len,length(tmp));
      end;
    end
    else exit;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
    substr,s:string;
begin
  substr:='AAA';
  s:='(dsfsAAA ghtyAAAghfg AAAgfhgfh AAA)fdgfd';
  i:=getLocation(substr,s);
  if i>0 then showmessage('在第 '+inttostr(i)+' 个位置找字符 “'+substr+'"。')
  else showmessage('字符串中找不到 “'+substr+'"。');
end;

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.084668159484863 seconds