count

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

Usage Scenarios

  • Data Analysis: When analyzing text data, count the frequency of specific keywords.
  • Text Processing: In text editors or search engines, count the number of occurrences of a search term.

Example

import { count } from 'funtool/string'

count('hello world, hello again', 'hello'); // ✅ 2
count('abc', 'x'); // ❌ 0

Signature

function count(str: string, substr: string): number

Parameters

  • str ( string ): The input string.
  • substr ( string ): The substring to count.

Returns

  • ( number ): The number of times the substring appears in the string.