Languages
[Edit]
EN

JavaScript - find character index in string

3 points
Created by:
Paris-Bateman
314

In this article, we would like to show you how to find a character in a string in JavaScript.

Quick solution:

'ABC'.indexOf('B')  // 1

 

Practical examples

To find the character in a string we need to use indexOf() method.

// ONLINE-RUNNER:browser;

const result = 'dirask is awesome!'.indexOf('d');
console.log(result); //0
// ONLINE-RUNNER:browser;

const myString = 'dirask is awesome!';
console.log(myString.indexOf('i')); //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.

References

JavaScript - String (popular problems)

JavaScript - find character index in string
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join