indexOf and lastIndexOf String Methods

preview_player
Показать описание
When you want to search inside one string for another string, JavaScript gives us two great methods - indexOf and lastIndexOf.

Рекомендации по теме
Комментарии
Автор

We can always learn something new and special from you, the last indexOf example is excellent. Thank you.

rotrose
Автор

Thank you so much for explaining these concepts. I have a better understanding of them now!

yolismazacarias
Автор

i believe a common use of the `Start Position` parameter is to find successive instances of the same item:

L=console.log

{
const sSentence = 'Be the change that you want to see.'
const sChar = 'e'
let aFound = FindItems(sSentence, sChar)
L(aFound)

function FindItems(sSentence, sChar)
{
let iFound, iStart, aFound = []
do
{
iFound = sSentence.indexOf(sChar, iStart)
if (iFound > -1) {
aFound.push(iFound)
iStart = iFound + 1
}
}
while (iFound > -1)
return aFound
}
}

But, would array.Filter be faster than this?

johnywhy
Автор

it's nice to know that we have LastIndexOf(), but I'm more curious When we will use it in real life? maybe soemthing like searching for latest input or so?

TiffanyNg
Автор

Hi. First of all... thanks for the video. It was pretty clear. Now, my question is... as a programmer yourself, which scenarios have you had to use them?

kikevanegazz
Автор

Thank you very much man!
can u plz tell me how the second parameter starts to look for the specified words when it comes down to lastIndexOf("string", 15) for instance?

ripplesr
Автор

what about lastIndexOf with the second parameter?

hindj
Автор

i realized in JS, both negative and positive numbers are truthy, and 0 is falsey. I can understand why indexOf returns -1 for not-found. It can't return 0, cuz that's a legitimate array-index.

Still, it seems somehow inappropriate for indexOf to return -1, since it's a nonsensical array-index, and -1 is truthy. Wondering if there's another value that would be more appropriate, like null, or undefined, or false?

johnywhy
Автор

Why would you store console.log() in a variable?

japeter
Автор

Very nice explanation but please improve sound quality. It's very low

amitgarg
Автор

Can someone explain this?
let x = ["Python", "C++", "C", "Java"];
let z = x.lastIndexOf("C", 1);
and its -1.... How??

holo
Автор

Entendi como usarlo aunque esta en ingles el audio

kodama