charCount
Counts how many times a specific character appears in a string.
Syntax
typescript
charCount(str: string, char: string): number
Parameters
Parameter | Type | Description |
---|---|---|
str | string | The string in which to count. |
char | string | The character to count (must be a single-character string). |
Returns
Type | Description |
---|---|
number | The number of occurrences of the character in the string. |
Example
typescript
charCount("banana", "a"); // 3
charCount("abracadabra", "b"); // 2
charCount("teste", "z"); // 0
Notes
- Throws
TypeError
ifstr
is not a string or ifchar
is not a single-character string. - The count is case-sensitive.