Delphi Tips&Tricks News   Tips   .NET Software   VCL Software   Search   Contacts
Ultimate Pack Special Offer!


Share this page

Follow us
LinkedIn Blogspot Twitter Facebook

Related products
Ultimate Pack  hot!
iGrid Plotter  new!
Image Editor
Runtime Fusion
Form Designer
Object Inspector
Print Suite Pro
Commented Image
Delphi Toys
WinDowse
Delphi Bonus
TMS Scripter Studio
Form Designer VB
Form Designer .NET
Print Suite .NET
Gradient Controls .NET  new!

Related links
Win32.hlp online version
MegaDetailed.NET
Delphi to C#

Special
Free Software Promotion
Offers for Resellers

Hobby projects
cdtrrracks.com
books.storrre.com
in3steps.com
sovietphillumeny.com

Compare string with pattern - Algorithms - Tips & Tricks - Greatis Delphi Pages

If you want to check your string with pattern, which has '?' symbol for changing any symbol, then you may use ComparePattern procedure. First parameter - is a mask, second - is your string and Check variable is result. Result is True, if your string matches with pattern.


procedure ComparePattern(St1, St2: string; var Check: Boolean);
var
  Hlp1, Hlp2: string;
  Pos1: Integer;
begin
  Check:=True;
  while Length(St1)>0 do
  begin
    Pos1:=Pos('?', St1);

    if (Pos1=0) then
    begin
      if CompareStr(St1, St2)<>0 then Check:=False;
      Delete(St1, 1, Length(St1));
      Delete(St2, 1, Length(St2));
    end;

    if (Pos1>0) then
    begin
      Hlp1:=Copy(St1,1,Pos1-1);
      Hlp2:=Copy(St2,1,Pos1-1);
      if CompareStr(Hlp1, Hlp2)<>0 then Check:=False;
      Delete(St1,1,Pos1);
      if Pos1>Length(St2) then Check:=False;
      Delete(St2,1,Pos1);
    end;

    if Check=False then Break;

  end;
  if CompareStr(St1, St2)<>0 then Check:=False;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Check: Boolean;
begin
  ComparePattern(Edit1.text, Edit2.Text, Check);
  if Check=True then Caption:='OK'
  else Caption:='Not OK';
end;
Related topics
Comparing of two strings

For more
Delphi Help

Download source