Articles   Members Online: 3
-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 get the number of matches when searching a string 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
20-Oct-02
Category
Object Pascal-Strings
Language
Delphi 2.x
Views
57
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

I want to check how many of the individual characters in a search string are 
matching a source string. When comparing the strings, each character position 
should be checked individually. Examples:
Seach string: 'ABC' / Source string: 'ABX' / Result: 2 matching positions
Seach string: 'ABC' / Source string: 'AXB' / Result: 1 matching position

Answer:

Solve 1:

Assuming the compare always starts from the beginnig of the source string, i.e. 
Search string: 'ABC' and Source string: 'AXAB' returns 1:

1   function MyStrComp(strSearch, strSource: string): integer;
2   var
3     LenSearch, LenSource, Len: integer;
4     LoopCounter: integer;
5   begin
6     LenSearch := length(strSearch);
7     LenSource := length(strSource);
8     if LenSearch > LenSource then
9       Len := LenSource
10    else
11      Len := LenSearch;
12    if Len = 0 then
13      Result := 0
14    else
15    begin
16      LoopCounter := 1;
17      repeat
18        if (strSearch[LoopCounter] <> strSource[LoopCounter] then
19          break;
20          Inc(LoopCounter);
21      until
22      (LoopCounter > Len);
23      Result := LoopCounter - 1;
24    end;
25  end;


The above function may not be the fastest, but it should be pretty fast. If you are 
going to call this compare function in a loop and the search string will not change 
within the loop, find the length of the search string before getting in the loop, 
then pass it to the compare function as a parameter can save a call to the length 
function in the compare function.

If you know that the source string is always longer than the search string, you can 
skip comparing the lengths and use the length of the search string to control your 
repeat loop.


Solve 2:

From AdvanceStringCore http://www.excommunicant.co.uk. You may use this routine for 
non commercial purposes providing appropriate credit is given.

26  {Concurrent matching char elements}
27  
28  function ConcurrentCountMatchingCharElements(const SourceChars, MaskChars: string;
29    const CSensitive: Boolean): Integer;
30  
31  {Version 1.0 (September 2000 - Lachlan Fairgrieve)
32  (C)2000 Excommunicant www.excommunicant.co.uk
33  Returns the number of times [mask[i]] and [source[i]] characters match}
34  
35  var
36    {initialise :
37    validate strings (for length)
38    Count Lengths
39    normalise lengths
40    Prepare Pointers (For case insensitive and standard search)
41    Source Scan : For Every [Source] Char check against every [mask] char}
42    NewSource, NewMask: string; {Hold normalised strings}
43    SourceP, MaskP: PChar;
44    index: Integer; {Source and Mask index vars}
45    ScanLength: Integer; {The number of chrs to scan}
46  begin
47    {Initialise}
48    Result := 0;
49    if SourceChars = '' then
50      exit;
51    if MaskChars = '' then
52      exit;
53    ScanLength := Length(SourceChars);
54    if Length(MaskChars) < ScanLength then
55      ScanLength := Length(MaskChars);
56    {prepare string pointers}
57    if not CSensitive then
58    begin
59      NewSource := uppercase(SourceChars);
60      NewMask := uppercase(MaskChars);
61      SourceP := Pointer(NewSource);
62      MaskP := Pointer(NewMask);
63    end
64    else
65    begin
66      SourceP := Pointer(SourceChars);
67      MaskP := Pointer(MaskChars);
68    end;
69    {source scan}
70    for index := 1 to ScanLength do
71    begin
72      if MaskP^ = SourceP^ then
73        inc(result);
74      inc(MaskP);
75      inc(SourceP);
76    end;
77  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