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

Last change on this file since 231 was 231, checked in by pinsard, 17 years ago

improvements/corrections of some *.pro headers

  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1;+
2; @file_comments
3; Count number of occurrences of a substring in a string.
4;
5; @categories
6; Text, String
7;
8; @param STRN {in}{required}{type=string}
9; The string in which to count occurences.
10;
11; @param SUBSTRN {in}{required}{type=string}
12; The substring to count occurrences of.
13;
14; @param STARTPOS {in}{required}
15; The position at which to begin the search.
16; If not supplied, start at beginning of string.
17;
18; @keyword HELP
19; Print useful message and return.
20;
21; @returns
22; Number of occurances of substring in string.
23;
24; @restrictions
25; Overlapping occurances are not counted separately.  For
26; example, counting occurances of 'bb' in 'blah bbb' returns one
27; occurance.
28;
29; @history
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;  Copyright (C) 1996 The Regents of the University of California, All
42;  Rights Reserved.  Written by Matthew W. Craig.
43;  See the file COPYRIGHT for restrictions on distrubting this code.
44;  This code comes with absolutely NO warranty; see DISCLAIMER for details.
45;
46; @version
47; $Id$
48;-
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.