5 total views
In order to find the correct index of a character , you can use
indexOf() method of string class.
Here is an example program to demonstrate the usage of this method.
<!DOCTYPE html> <html> <head> <title>JS: indexOf Method</title> <meta charset="utf-8"> </head> <body> <script> var answer = "Lion is the king of Jungle!"; var res = answer.indexOf('king'); console.log(res); //output = 12 </script> </body> </html>
The substring ‘king’ starts at index 12. The script goes through all the characters one-by-one and finally reaches the character ‘k’ which is at index 12.