Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to Determin if a string matches a pattern with wildcards ('?' and '*') Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
21-May-03
Category
Object Pascal-Strings
Language
Delphi All Versions
Views
125
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Ernesto De Spirito

Is there a LIKE function in Delphi that compares a string with a pattern?

Answer:

Solve 1:

Sometimes we need to know if a string matches a pattern, which is a string with 
wildcards (for example '?' and '*'). Here we implement a function that returns True 
if the string matches the pattern and False if not. 

1   function Like(AString, Pattern: string): boolean;
2   var
3     i, n, n1, n2: integer;
4     p1, p2: pchar;
5   label
6     match, nomatch;
7   begin
8     AString := UpperCase(AString);
9     Pattern := UpperCase(Pattern);
10    n1 := Length(AString);
11    n2 := Length(Pattern);
12    if n1 < n2 then
13      n := n1
14    else
15      n := n2;
16    p1 := pchar(AString);
17    p2 := pchar(Pattern);
18    for i := 1 to n do
19    begin
20      if p2^ = '*' then
21        goto match;
22      if (p2^ <> '?') and (p2^ <> p1^) then
23        goto nomatch;
24      inc(p1);
25      inc(p2);
26    end;
27    if n1 > n2 then
28    begin
29      nomatch:
30      Result := False;
31      exit;
32    end
33    else if n1 < n2 then
34    begin
35      for i := n1 + 1 to n2 do
36      begin
37        if not (p2^ in ['*', '?']) then
38          goto nomatch;
39        inc(p2);
40      end;
41    end;
42    match:
43    Result := True;
44  end;


Sample call 

45  if Like('Walter', 'WA?T*') then
46    ShowMessage('It worked!');


If you want to see another example, we use this function to determine if a file 
name matches a specification in the article "Determining if a file name matches a 
specification" (keyword: MatchesSpec). 


Solve 2:

There is a built in Delphi function called MatchesMask(). It takes * , ? and sets 
as parameters. 

47  {...}
48  if MatchesMask('Hello World', '[H-K]?????[W-Y]*') then
49    
50  {...}
51  if MatchesMask(FileName, '*.exe') then
52    
53  {...}


Copyright (c) 2001 Ernesto De Spiritomailto:edspirito@latiumsoftware.com
Visit: http://www.latiumsoftware.com/delphi-newsletter.php

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC