source: trunk/STRING/strcnt.pro @ 2

Last change on this file since 2 was 2, checked in by opalod, 22 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 KB
Line 
1;
2;+
3; NAME:
4;       STRCNT
5; PURPOSE:
6;       Count number of occurrences of a substring in a string.
7; CATEGORY:
8;       text/strings
9; CALLING SEQUENCE:
10;       num = strcnt(strn, substring, [pos])
11; INPUTS:
12;       string    -- The string in which to count occurences.     in
13;       substring -- The substring to count occurrences of.       in
14;       pos       -- the position at which to begin the search.   [in]
15;                    If not supplied, start at beginning of
16;                    string.
17; KEYWORD PARAMETERS:
18;       /HELP     -- Print useful message and return.
19; OUTPUTS:
20;       num       -- Number of occurances of substring in string. out
21; COMMON BLOCKS:
22; SIDE EFFECTS:
23; NOTES:
24;       Overlapping occurances are not counted separately.  For
25;       example, counting occurances of 'bb' in 'blah bbb' returns one
26;       occurance.
27; EXAMPLE:
28; MODIFICATION HISTORY:
29;       $Id$
30;       $Log$
31;       Revision 1.1  2002/09/11 10:17:16  opalod
32;       Initial revision
33;
34;       Revision 1.3  1996/06/14 20:00:27  mcraig
35;       Updated Copyright info.
36;
37;       Revision 1.2  1996/05/09 00:22:17  mcraig
38;       Added fast processing using BYTE arrays if we are counting occurences of
39;       a single character.  Added error handling.
40;
41;       Revision 1.1  1996/01/31 18:47:37  mcraig
42;       Initial revision
43;
44; RELEASE:
45;       $Name$
46;
47; COPYRIGHT:
48;  Copyright (C) 1996 The Regents of the University of California, All
49;  Rights Reserved.  Written by Matthew W. Craig.
50;  See the file COPYRIGHT for restrictions on distrubting this code.
51;  This code comes with absolutely NO warranty; see DISCLAIMER for details.
52;-
53FUNCTION Strcnt, strn, substrn, startpos, $
54                 HELP=Help
55
56; Return to caller if error.
57    On_error, 2
58
59; Help user, if needed.
60    IF (n_params() LT 2) OR keyword_set(Help) THEN BEGIN
61        offset = '   '
62        print, offset+'Count number of occurrences of a substring in a string.'
63        print, offset+'num = strcnt(strn, substring, [pos])'
64        print, offset+'Inputs:'
65        print,offset+offset+'string    -- The string in which to count occurences.     in'
66        print,offset+offset+'substring -- The substring to count occurrences of.       in'
67        print,offset+offset+'pos       -- the position at which to begin the search.   [in]'
68        print,offset+offset+'             If not supplied, start at beginning of'
69        print,offset+offset+'             string.'
70        print, offset+'Keywords:'
71        print,offset+offset+'/HELP     -- Print useful message and return.'
72        print, offset+'Outputs:'
73        print,offset+offset+'num       -- Number of occurances of substring in string. out'
74        return, -1
75    ENDIF
76
77    IF n_params() EQ 2 THEN startpos = 0
78
79;  return if we weren't really given a substring to search for. . .
80    IF strlen(substrn) EQ 0 THEN BEGIN
81        print, "Error: Can't count occurances of null string."
82        return, -1
83    ENDIF
84
85; . . .or if we were told to start at the end of the string.
86    tmpstrn = strmid(strn, startpos, strlen(strn))
87    IF strlen(tmpstrn) EQ 0 THEN return, 0
88
89; If looking for occurences of single character, process using BYTE
90; array.
91    IF strlen(substrn) EQ 1 THEN BEGIN
92        tmpstrn = byte(TmpStrn)
93        count = n_elements(where(TmpStrn EQ (byte(substrn))(0)))
94    ENDIF ELSE BEGIN
95        count = 0L
96        pos = rstrpos(tmpstrn, substrn)
97        WHILE pos GE 0 DO BEGIN
98            count = count + 1
99            pos = rstrpos(tmpstrn, substrn, pos)
100        ENDWHILE
101    ENDELSE
102
103    return, count
104END
105
106
107
108
109
110
111
112
Note: See TracBrowser for help on using the repository browser.