xxxxxxxxxx
var str = "We got a poop cleanup on isle 4.";
var strPos = str.indexOf("5");//9
xxxxxxxxxx
//indexOf getting index of array element, returns -1 if not found
var colors=["red","green","blue"];
var pos=colors.indexOf("blue");//2
//indexOf getting index of sub string, returns -1 if not found
var str = "We got a poop cleanup on isle 4.";
var strPos = str.indexOf("poop");//9
xxxxxxxxxx
const fruits = ["apple", "banana", "cantaloupe", "blueberries", "grapefruit"];
const index = fruits.findIndex(fruit => fruit === "blueberries");
console.log(index); // 3
console.log(fruits[index]); // blueberries
xxxxxxxxxx
let monTableau = ['un', 'deux','trois', 'quatre'] ;
console.log(monTableau.indexOf('trois')) ;
xxxxxxxxxx
// indexOf method only requires a single value
// and returns the index of the FIRST value found in array
const cities = [
"Orlando",
"Dubai",
"Denver",
"Edinburgh",
"Chennai",
"Accra",
];
console.log(cities.indexOf("Chennai"));
// expected output is 4
xxxxxxxxxx
const mainString = "Hello, this is a sample string with a sample substring.";
const searchString = "sample";
const index = mainString.indexOf(searchString);
if (index !== -1) {
console.log(`Substring "${searchString}" found at index ${index}`);
} else {
console.log(`Substring "${searchString}" not found`);
}
xxxxxxxxxx
public int indexOf(int value)
{
int index = -1;
for (int i = 0; i < size; i++)
{
if (elementData[i] == value)
{
index = i;
}
}
return index;
}
xxxxxxxxxx
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"