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
Counting occurrences in 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
22-May-03
Category
Object Pascal-Strings
Language
Delphi All Versions
Views
35
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Ernesto De Spirito

A function that returns the number of times a substring occurs in a string. There's 
also an ANSI version.

Answer:

The following functions return the number of occurrences of a char or a substring 
within a string or ANSI string: 

1   interface
2   
3   function Occurs(const str: string; c: char): integer; overload;
4   function Occurs(const str: string; const substr: string): integer;
5   overload;
6   function AnsiOccurs(const str: string; const substr: string): integer;
7   
8   implementation
9   
10  uses sysutils;
11  
12  function Occurs(const str: string; c: char): integer;
13  // Returns the number of times a character occurs in a string
14  var
15    p: PChar;
16  begin
17    Result := 0;
18    p := PChar(Pointer(str));
19    while p <> nil do
20    begin
21      p := StrScan(p, c);
22      if p <> nil then
23      begin
24        inc(Result);
25        inc(p);
26      end;
27    end;
28  end;
29  
30  function Occurs(const str: string; const substr: string): integer;
31  // Returns the number of times a substring occurs in a string
32  var
33    p, q: PChar;
34    n: integer;
35  begin
36    Result := 0;
37    n := Length(substr);
38    if n = 0 then
39      exit;
40    q := PChar(Pointer(substr));
41    p := PChar(Pointer(str));
42    while p <> nil do
43    begin
44      p := StrPos(p, q);
45      if p <> nil then
46      begin
47        inc(Result);
48        inc(p, n);
49      end;
50    end;
51  end;
52  
53  function AnsiOccurs(const str: string; const substr: string): integer;
54  // Returns the number of times a substring occurs in a string
55  // ANSI version
56  var
57    p, q: PChar;
58    n: integer;
59  begin
60    Result := 0;
61    n := Length(substr);
62    if n = 0 then
63      exit;
64    q := PChar(Pointer(substr));
65    p := PChar(Pointer(str));
66    while p <> nil do
67    begin
68      p := AnsiStrPos(p, q);
69      if p <> nil then
70      begin
71        inc(Result);
72        inc(p, n);
73      end;
74    end;
75  end;


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