/*To search for the index of a substring or string within an array,
use .indexOf()
If you search for something that isn't there, the function will return -1*/
let mechs = ["madcat", "blood asp", "atlas"];
console.log(mechs.indexOf("madcat"); //returns 0
console.log(mechs.indexOf("atlas"); //returns 2
let clan = "nova cat";
console.log(clan.indexOf("a")); /*returns 3 (Only the first instance of the
character is used.*/
console.log(clan.indexOf("cat")); /*returns 5 (Finds the index of the beginning
character)*/
console.log(clan.indexOf("s")); //returns -1
let str = "Please locate where 'locate' occurs!";
str.indexOf("locate");
//it will returns the index of (the position of) the first occurrence of a specified text in a string:
// >> 7
//if you find this answer is useful ,
//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
//indexOf()
const arr2 = [1,2,567,4,5]
//Below the indexOf searches for the value '567'
const findIndexOfValue567 = arr2.indexOf(567);
//this is then returned as an index
console.log(findIndexOfValue567)
//if the argument you place in the method is not there...
const isThisValueThere = arr2.indexOf(989);
//then a value of -1 is returned
console.log(isThisValueThere)
let monTableau = ['un', 'deux','trois', 'quatre'] ;
console.log(monTableau.indexOf('trois')) ;
public int indexOf(int value)
{
int index = -1;
for (int i = 0; i < size; i++)
{
if (elementData[i] == value)
{
index = i;
}
}
return index;
}
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf(searchTerm);
console.log(`The index of the first "${searchTerm}" from the beginning is ${indexOfFirst}`);
// expected output: "The index of the first "dog" from the beginning is 40"
console.log(`The index of the 2nd "${searchTerm}" is ${paragraph.indexOf(searchTerm, (indexOfFirst + 1))}`);
// expected output: "The index of the 2nd "dog" is 52"