countof()countof()

计算字符串中子字符串的出现次数。Counts occurrences of a substring in a string. 纯字符串匹配项可能重叠;正则表达式匹配项不会重叠。Plain string matches may overlap; regex matches do not.

countof("The cat sat on the mat", "at") == 3
countof("The cat sat on the mat", @"\b.at\b", "regex") == 3

语法Syntax

countof(text, search [, kind])countof(text, search [, kind])

参数Arguments

  • text:一个字符串。text: A string.
  • search:用于在 text 内部匹配的纯字符串或正则表达式search: The plain string or regular expression to match inside text.
  • kind"normal"|"regex" 默认值为 normalkind: "normal"|"regex" Default normal.

返回Returns

搜索字符串可在容器中匹配的次数。The number of times that the search string can be matched in the container. 纯字符串匹配项可能重叠;正则表达式匹配项不会重叠。Plain string matches may overlap; regex matches do not.

示例Examples

函数调用Function call 结果Result
countof("aaa", "a") 33
countof("aaaa", "aa") 3 (不是 2!)3 (not 2!)
countof("ababa", "ab", "normal") 22
countof("ababa", "aba") 22
countof("ababa", "aba", "regex") 11
countof("abcabc", "a.c", "regex") 22