DELPHI wildcard comparison
Author: Li Junyu email: [email protected] or [email protected] or [email protected]
It is recognized that DELPHI has stronger functions than VB, but sometimes it is not as convenient as VB in some small details. For example, there is SENDKEY() in VB, which can easily simulate keyboard keys, but it is more complicated in DELPHI. Another example is There is LIKE in VB to easily implement wildcards Compare, but I can't find it in DELPHI. For this reason, when I am in a free state and still in the mood, I make a custom function to realize this function, just in case I am not in the mood to do such a small thing again in an emergency. Program. This kind of small program is useless on the shelf. It is better to take it out and let it emit some light and heat.
The algorithm of the program first adds '?' to the end of the substring. *', then read the substring, look for the characters between the wildcard characters in the substring, that is, the substring in the substring, and then search in the source string to see if it contains the substring in the substring, but it is still very difficult to implement. Take some trouble.
function isABClikeAX(abc,ax:string):boolean; //abc is the source string, ax is the substring
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;
Pay attention to USES MATH, because MIN() is used, you can also use an IF statement to replace MIN(), but it is not clear enough.