COUNT()

COUNT(), COUNTS()

Top  Previous  Next

 

The COUNT() function counts occurrences of a substring within a string. The COUNTS() function is similar to COUNT() but operates on successive elements of a dynamic array, returning a similarly structured dynamic array of results.

 

 

Format

 

COUNT(string, substring)

COUNTS(string, substring)

 

where

 

stringevaluates to the string in which substrings are to be counted.

 

substringevaluates to the substring to count.

 

 

The COUNT() function counts occurrences of substring within string. Substrings may not overlap, thus

 

S = "ABABABABABAB"

N = COUNT(S, "ABA")

 

sets N to 3.

 

If substring is null, COUNT() returns the length of string.

 

Programs compiled with the $NOCASE.STRINGS compiler directive use case insensitive string comparisons in the COUNT() and COUNTS() functions.

 

The default behaviour of the COUNT() function is that overlapping substrings are not allowed. Thus

X = COUNT("aaaaaaaaa", "aa")

yields a result of 4.

 

The COUNT.OVERLAP setting of the $MODE compiler directive can be used to select an alternative behaviour in which substrings may overlap. The same example would then yield a result of 8.

 

 

Example

 

MARKS = COUNT(REC, @FM)

 

This statement counts the field marks in REC.

 

 

See also:

DCOUNT()