findIndex

Finds the first index of the substr in the str. If not found, returns -1.

Usage Scenarios

  • Text Search: In text processing, find the position of a specific substring.
  • Data Validation: Check if a substring exists in a string and get its position.

Example

import { findIndex } from 'funtool/string'

findIndex('hello world', 'world'); // ✅ 6
findIndex('abc', 'z'); // ❌ -1

Signature

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

Parameters

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

Returns

  • ( number ): The index of the first occurrence of the substring, or -1 if not found.