function strfind
strfind(
str: string,
pattern: string,
): array

Finds all occurrences of a substring within a string.

Returns an array of indices where the search pattern is found within the string. If the pattern's length is greater than the string's length, or if the inputs are invalid, an error is thrown.

Examples

Basic usage with multiple occurrences

import { assertEquals } from "jsr:@std/assert";

assertEquals(strfind('find indices in the string', 'in'), [1, 5, 13, 23]);

Pattern appears only once

import { assertEquals } from "jsr:@std/assert";

assertEquals(strfind('hello world', 'world'), [6]);

Pattern does not appear

import { assertEquals } from "jsr:@std/assert";

assertEquals(strfind('hello world', 'notfound'), []);

Pattern is longer than the string

import { assertEquals } from "jsr:@std/assert";

assertEquals(strfind('short', 'longpattern'), []);

Parameters

str: string

The string to be searched.

pattern: string

The search pattern.

Return Type

An array of indices where the pattern occurs in the string.

Throws

If the input arguments are invalid or the pattern is longer than the string.