Mega Code Archive

 
Categories / Delphi / Functions
 

Like function

Title: Like function Question: Simulating Find-Like function Find-File Answer: This function finds a string similar to a substring.Yuo can use (*) and (?) instead of letters.This function extends the Like function previously posted to Delphi3000.I needed the same capability as the Find File function. //Like function Like(AString,Pattern:string):boolean; var n,n1,n2,i,j,n2n1:integer; p1,p2:pchar; begin result:=true; n1:=length(AString);n2:=Length(Pattern); if (n1+1) AString:=lowercase(AString);Pattern:=lowercase(pattern); p1:=pchar(AString);p2:=pchar(Pattern); if n1 //check the pos of '*' if pos('*',Pattern)=1 then begin //'*' otpred for i:=n downto 2 do if ((p2+i-1)^'?')and((p1+i+n2n1-1)^(p2+i-1)^) then begin result:=false;break; end//if end else begin //'*' not otpred if pos('*',Pattern)=n2 then begin//posleden '*' for j:=0 to n-2 do if ((p2+j)^'?') and ((p1+j)^(p2+j)^) then begin result:=false;break; end //if end else //ako ima po stredata if n1=n2 then begin for j:=0 to n-1 do if ((p2+j)^'?') and ((p1+j)^(p2+j)^) then begin result:=false;break; end end else //n1n2 result:=false; end;//else end;