DELPHI的通配符比較
作者:李均宇email: [email protected]或[email protected]或[email protected]
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 )) 或 ((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(),但不夠明白.