COUNT function
Syntax
COUNT (string, substring)
Description
Use the COUNT function to return the number of times a substring is repeated in a string value.
string is an expression that evaluates to the string value to be searched. substring is an expression that evaluates to the substring to be counted. substring can be a character string, a constant, or a variable.
If substring does not appear in string, a 0 value is returned. If substring is an empty string, the number of characters in string is returned. If string is the null value, null is returned. If substring is the null value, the COUNT function fails and the program terminates with a run-time error message.
By default, each character in string is matched to substring only once. Therefore, when substring is longer than one character and a match is found, the search continues with the character following the matched substring. No part of the matched string is recounted toward another match. For example, the following statement counts two occurrences of substring TT and assigns the value 2 to variable C:
C = COUNT ('TTTT', 'TT')
PICK, IN2, and REALITY Flavors
In PICK, IN2, and REALITY flavors, the COUNT function continues the search with the next character regardless of whether it is part of the matched string. For example, the following statement counts three occurrences of substring TT:
C = COUNT ('TTTT', 'TT')
Use the COUNT.OVLP option of the $OPTIONS statement to get this behavior in IDEAL and INFORMATION flavor accounts.
Example
A=COUNT('ABCAGHDALL','A')
PRINT "A= ",A
*
Z='S#FF##G#JJJJ#'
Q=COUNT(Z,'#')
PRINT "Q= ",Q
*
Y=COUNT('11111111','11')
PRINT "Y= ",Y
This is the program output:
A= 3
Q= 5
Y= 4