JavaScript | String indexOf() Method

JavaScript | String indexOf() Method

JavaScript: String indexOf() Method .javascript find occurrence of character in string. This tutorial explains to you how you can find the character in a given string using the javascript string indexOf() method.

javascript find occurrence of character in string. This tutorial explains to you how you can find the character in a given string using the javascript string indexOf() method.

In this tutorial, you also learn javascript find the occurrence of the character in the string.

JavaScript: String indexOf() Method

Definition:- The javascript string method indexOf(), which is used to find the first occurrence of a specified value in a given string.

Note:- This string method returns -1 if the value to search is not found in the given string.

Syntax:

The basic syntax of js string indexOf() method is:

str.indexOf( search_value, start)

Here,

  • Str:- It’s a given string
  • search_value:- This is a required parameter. String to search in a given string
  • start:- This is an optional parameter. Where to start the search in a given string

Ex1:

Let’s take first example of indexOf() method in javascript. Let you have one string like “Hello developers, welcome to the javascript tutorial.”. In this string, you want to find the first “de” character. Let’s see the below example:

var string = "Hello developers, welcome to the javascript tutorial.";
var res = string.indexOf("ve");
document.write('Result of the above code is:-' + res);

Result of the above code is:-8

Ex2:

Now we take a second example with a specific position. Let you have one string like above. And you want to find string or characters in a given string with a specific position. Let’s see the below-given example:

var string = "Hello javascript developers, welcome to the javascript tutorial.";
var res = string.indexOf("javascript", 10);
document.write('Result of the above code is:-' + res);

Result of the above code is:-44

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Java vs. JavaScript: Know The Difference

Java vs. JavaScript: Know the Difference, Java vs. JavaScript: What's the Difference? Java vs. JavaScript: Major Similarities and Differences. pros and cons of JavaScript and Java.

Programming a Javascript Simon Game Tutorial

In this javascript tutorial, I recorded myself live programming an html5 javascript simon game.

Understanding Memoization And Dynamic Programming in Javascript

In this Javascript tutorial I will explain what memoization is, how to use it, when you should use memoization, how to use memoization, what dynamic programming is, how to use memoization in dynamic programming. Memoization is a big complicated word that you may have never even heard before, but you may be surprised to know that you are most likely already using memoization without even realizing it.