Description

Counts the number of occurrences of a substring in a string.

Syntax

instances = Count (source_string, search_string)

Parameters

The Count function has the following parameters.

ParameterDescription
source_stringIdentifies the string to search for in search_string. Can be a literal string, expression, constant, or variable.
search_stringIdentifies the string to search for in source_string. search_string may be a literal string, expression, constant, or variable.

If search_string does not appear in source_string, or if search_string is null, a 0 (zero) will be returned.

The Count() and DCount() functions are usually used to determine the size of a dynamic array. They actually have a more general purpose use, because the search string can be any string used as a delimiter, even multiple character strings.

Note: To count the number of values in a dynamic array, you need to account for the possibility that the array has one value, with no delimiter. The count() function in this case will return 0 because there is no delimiter in the string. To return the correct answer (1), you need to add the boolean expression ( var # ), which returns 1 if the variable contains a value but 0 if it is null. See the example below.

Note: If you use DCount() to count the number of values in a dynamic array, you do not need to add the boolean expression because DCount() returns the proper result (1) if the array has one value with no delimiter.

See Also

DCount()

Example

* count the number of items in an @fm-delimited list
cnt = count(List, @fm) + (List# '')
* this is equivalent to:
declare function dcount
cnt = dcount(List, @fm)
  • No labels