DEV Community

Discussion on: What happens when you IndexOf an empty string?

Collapse
 
merri profile image
Vesa Piittinen • Edited

You can give position as indexOf second parameter. This gives a plausible little case where you can figure out the length of a string without using length:

'abc'.indexOf('', Infinity)

Most likely useful use case I can think of would be when doing code golfing.

Other than that, I think the current functionality is perfectly fine. It is often a silly case, but I don't see a reason to block it. It does give a valid match to positions between and around characters.

Collapse
 
turnerj profile image
James Turner

Interesting idea using it to find out the length though that trick only works for JavaScript. In C#, you get an ArgumentOutOfRangeException unless you specify the start position to be within the length of the string.

I'm not sure how useful it would be for finding the position between characters as it can only tell you what the start position was.