source: trunk/SRC/Textoidl/strcnt.pro @ 114

Last change on this file since 114 was 114, checked in by smasson, 18 years ago

new compilation options (compile_opt idl2, strictarrsubs) in each routine

  • Property svn:executable set to *
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: strcnt.pro,v 1.3 1996/06/14 20:00:27 mcraig Exp $
30;       $Log: strcnt.pro,v $
31;       Revision 1.3  1996/06/14 20:00:27  mcraig
32;       Updated Copyright info.
33;
34;       Revision 1.2  1996/05/09 00:22:17  mcraig
35;       Added fast processing using BYTE arrays if we are counting occurences of
36;       a single character.  Added error handling.
37;
38;       Revision 1.1  1996/01/31 18:47:37  mcraig
39;       Initial revision
40;
41; RELEASE:
42;       $Name: Rel_2_1_2 $
43;
44; COPYRIGHT:
45;  Copyright (C) 1996 The Regents of the University of California, All
46;  Rights Reserved.  Written by Matthew W. Craig.
47;  See the file COPYRIGHT for restrictions on distrubting this code.
48;  This code comes with absolutely NO warranty; see DISCLAIMER for details.
49;-
50FUNCTION Strcnt, strn, substrn, startpos, $
51                 HELP=Help
52;
53  compile_opt idl2, strictarrsubs
54;
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.