charCount
Counts how many times a specific character appears in a string.
Syntax
typescript
charCount(str: string, char: string): numberParameters
| 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"); // 0Notes
- Throws
TypeErrorifstris not a string or ifcharis not a single-character string. - The count is case-sensitive.
