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 Find a substring in a TStrings 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
19-Nov-02
Category
Object Pascal-Strings
Language
Delphi 2.x
Views
75
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: William Gerbert

How to find a substring in a TStrings

Answer:

The IndexOf function in TStrings is great because it lets you quickly get the index 
of Item that holds the string in question. Unfortunately, it doesn't work for 
sub-strings. In that case, I've put together a neat little function called 
IndexOfSubString where you pass in the TStrings descendant you want to search on 
and a search value, and it'll return the index. Check it out:
1   
2   {Purpose  : Binary search algorithm for a
3               TStrings object. Finds the first
4               occurence of any substring within
5               a TStrings object or descendant}
6   
7   function IndexOfSubString(List: TStrings; SubString: string): Integer;
8   var
9     I,
10      LowIdx,
11      HighIdx: Integer;
12    Found: boolean;
13  begin
14    Found := false;
15    Result := -1;
16    {This type of search uses the first half
17     of the TStrings list, so initialize the
18     LowIdx and HighIdx to the first and approximate
19     half of the list, respectively.}
20    LowIdx := 0;
21    HighIdx := List.Count div 2;
22  
23    {Note that Found and the LowIdx are used
24     as conditionals. It's obvious why Found
25     is used, but less apparent why LowIdx is
26     used instead of HighIdx. The reason for
27     this is that the way I've set it up here,
28     HighIdx will never exceed (List.Count - 2),
29     whereas LowIdx can equal (List.Count - 1)
30     by nature of the assignment
31     if Found remains false after the for loop.}
32    while not Found and (LowIdx < (List.Count - 1)) do
33    begin
34      for I := LowIdx to HighIdx do
35        if (Pos(SubString, List[I]) > 0) and
36          not Found then
37        begin
38          Found := true;
39          Result := I;
40        end;
41  
42      if not Found then
43      begin
44        LowIdx := HighIdx + 1;
45        HighIdx := HighIdx + ((List.Count - HighIdx) div 2);
46      end;
47    end;
48  end;


			
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