DELPHI的通配符比较
作者:李均宇 email: e271828@163.net或者MSSQLserver2000@163.com或者OKMYdelphi@163.net
DELPHI的功能比VB强是公认的,但在一些小事小节上却有时比不上VB方便,例如VB中有SENDKEY()可以很方便地模拟键盘按键,但在DELPHI中就比较繁些.又如VB中有LIKE来轻易实现通配符的比较,但在DELPHI中却找不到,为此在处于自由状态下的我尚有心情时便自已动手来作一个自定义的函数来实现这个功能,以防应急时无心情再作这种小程序.这种小程序束之高阁无用,不如拿出来让它发出几分光和热.
程序的算法先在子串的末尾加上‘?*’,再读取子串,查找子串中的通配符之间的字符,亦即子串中的子串,然后在源串中依次查找是否含有子串中的子串,不过实现起来还是十分费些周折.
function isABClikeAX(abc,ax:string):boolean; //abc是源串,ax是子串
var
abcstart,axstart,abclength,axlength:integer;
endpartabc,endpartax,subax:string;
temp,abcwww,axwww:integer;
begin //aaa
temp:=0;
abcstart:=1;
axstart:=1;
axwww:=1;
abcwww:=1;
abclength:=length(abc);
axlength:=length(ax);
isabclikeax:=true;
while axstart<=axlength do
begin//bbb
if ax[axstart]='?' then
begin
inc(axstart);
inc(abcstart);
if abcstart> abclength then
begin
isabclikeax:=false;
break;
end;
continue;
end;
if ax[axstart]='*' then
begin
inc(axstart);
temp:=1;
axwww:=axstart;
abcwww:=abcstart;
continue;
end;
if not(ax[axstart] in ['?','*'] ) then
begin//ccc
endpartax:=copy(ax,axstart,axlength-axstart+1)+'?*';
subax:=copy(endpartax,1,min(pos('?',endpartax),pos('*',endpartax))-1);
axstart:=axstart+min(pos('?',endpartax),pos('*',endpartax))-1;
endpartabc:=copy(abc,abcstart,abclength-abcstart+1);
if ((pos(subax,endpartabc)<>0) and (temp=1 )) or ((pos(subax,endpartabc)=1) and (temp=0)) then
begin //ddd
if temp=1 then temp:=0;
abcstart:=abcstart+(pos(subax,endpartabc)+length(subax)-1) ;
// axstart:=axstart+min(pos('?',endpartax),pos('*',endpartax))-1;
end //ddd
else //ddd
begin //ddd
if temp=0 then
begin
axstart:=axwww;
abcwww:=abcwww+1;
abcstart:=abcwww;
temp:=1;
continue;
end;
isabclikeax:=false;
break;
end; //ddd
end;//ccc
end;//bbb
end;//aaa
FUNCTION islike(abc,ax:string):boolean;
begin
islike:=isABClikeAX(abc,ax);
end;
FUNCTION widecard(abc,ax:string):boolean;
begin
widecard:=isABClikeAX(abc,ax);
end;
注意USES MATH,因为用到MIN(),也可以用IF语句来代替MIN(),但不够明白.