本文整理自网络,侵删。
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;
相关阅读 >>
Delphi 时间差函数及部分字符串与日期时间相互转换的函数
更多相关阅读请进入《Delphi》频道 >>