本文整理自网络,侵删。
1.PosEx
功能说明:实现正向增强查询,跟FastCodePosEx功能相同,不依赖fastCode;
OffSet为偏移位置,通过测试,速度要比FastCodePosEx稍慢。
function PosEx(const SubStr,S:AnsiString;const Offset:Cardinal=1):Integer;
var
iPos: Integer;
i, j,LenS,LenSub: Integer;
PCharS, PCharSub: PChar;
begin
Result := 0;
LenS:=Length(S);
lenSub := length(Substr);
if (LenS=0) Or (lenSub=0) then Exit;
if (LenS-lenSub)<Offset then Exit;
PCharS := PChar(s);
PCharSub := PChar(Substr);
for I := Offset-1 to LenS-1 do
begin
if lens-I<LenSub then Exit;
for j := 0 to lenSub - 1 do
if PCharS[i + j] <> PCharSub[j] then
break
else if J=LenSub-1 then
begin
Result:=I+1;
Exit;
end;
end;
end;
2.PosRightEx
功能说明:实现反向增强查询;
OffSet为偏移位置,通过指定Offset,只查询Offset以前位置(包括Offset所在位置字符)。查询方法为从右向左最比。与PosEx效率相同。
function PosRightEx(const SubStr,S:AnsiString;const Offset:Cardinal=MaxInt):Integer;
var
iPos: Integer;
i, j,Len,LenS,LenSub: Integer;
PCharS, PCharSub: PChar;
begin
Result := 0;
LenS:=Length(S);
lenSub := length(Substr);
if (LenS=0) Or (lenSub=0) then Exit;
if Offset<LenSub then Exit;
PCharS := PChar(s);
PCharSub := PChar(Substr);
Len:=Offset;
if Len>LenS then
Len:=LenS;
for I := Len-1 downto 0 do
begin
if I<LenSub-1 then Exit;
for j := lenSub - 1 downto 0 do
if PCharS[i -lenSub+j+1] <> PCharSub[j] then
break
else if J=0 then
begin
Result:=I-lenSub+2;
Exit;
end;
end;
end;
以上两个函数都是大小写敏感的,可以通过自己修改对比方法。
相关阅读 >>
Delphi xe5 将Delphi code从winos 迁移到ios与android
Delphi执行sql提示“不正常地定义参数对象”,“提供了不一致或不完整的信息”错误
Delphi access数据库密码的mdb的访问报错“无法启动应用程序,或是已被其他用户已独占方式打开”
Delphi extractclassname 字符串处理函数
更多相关阅读请进入《Delphi》频道 >>