count method

int count(
  1. String find
)

Counts occurrences of find in this string.

Uses a split-based approach that counts non-overlapping matches only. For example, counting 'aa' in 'aaa' returns 1, not 2.

Note: This method counts non-overlapping matches. Consider adding an allowOverlap parameter if overlapping occurrences are desired.

Args:

  • find: The substringSafe to count. Returns 0 if empty.

Returns: The number of non-overlapping occurrences of find.

Example:

'hello'.count('l'); // 2
'aaa'.count('aa'); // 1 (non-overlapping)
'test'.count('x'); // 0
'hello'.count(''); // 0

Implementation

int count(String find) {
  if (find.isEmpty) return 0;
  return split(find).length - 1;
}