countof()
Learn how to use the countof() function to count the occurrences of a substring in a string.
Counts occurrences of a substring in a string. Plain string matches may overlap; regex matches don’t.
Syntax
countof(source, search [, kind])
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| source | string | ✔️ | The value to search. |
| search | string | ✔️ | The value or regular expression to match inside source. |
| kind | string | The value normal or regex. The default is normal. |
Returns
The number of times that the search value can be matched in the source string. Plain string matches may overlap; regex matches don’t.
Examples
| Function call | Result |
|---|---|
countof("aaa", "a") | 3 |
countof("aaaa", "aa") | 3 (not 2!) |
countof("ababa", "ab", "normal") | 2 |
countof("ababa", "aba") | 2 |
countof("ababa", "aba", "regex") | 1 |
countof("abcabc", "a.c", "regex") | 2 |
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.