EN
TypeScript - find character index in string
0 points
In this article, we would like to show you how to find a character in a string in TypeScript.
Quick solution:
xxxxxxxxxx
1
'ABC'.indexOf('B') // 1
To find the character in a string we need to use indexOf()
method.
xxxxxxxxxx
1
const result: number = 'dirask is awesome!'.indexOf('d');
2
3
console.log(result); // 0
Output:
xxxxxxxxxx
1
0
xxxxxxxxxx
1
const myString: string = 'dirask is awesome!';
2
3
console.log(myString.indexOf('i')); // 1
Output:
xxxxxxxxxx
1
1
Note:
If there is more than one occurrence of the character, the
infexOf()
method returns the position of the first one, starting from the left.