find the first index of a substring in a string in typescript

To find the first index of a substring in a string in TypeScript, you can use the indexOf() method on the string. This method returns the index of the first occurrence of the specified substring, or -1 if the substring is not found. Here's an example:

index.ts
const str: string = "hello world";
const substr: string = "wo";

const index: number = str.indexOf(substr);

console.log(index); // Output: 6
142 chars
7 lines

In this example, we first define a string str and a substring substr. We then call the indexOf() method on str and pass in substr as the argument. The method returns the index of the first occurrence of substr in str, which is 6. Finally, we log the result to the console.

gistlibby LogSnag