4 ms·
I'd rather have this invariant: haystack[haystack.indexOf(needle)] == needle That is, there is no index of the empty string within a string. A nice thing ab
by l0b0 3y ago
I'd rather have this invariant:
haystack[haystack.indexOf(needle)] == needle
That is, there is no index of the empty string within a string. A nice thing about this is that it also works for non-character arrays:
"abc".indexOf("") raises "No such entry exception"
["", "abc", ""].indexOf("") == 0
- ajuc 3y agoThis invariant only works for 1-character needles. So no "Lorem Ipsum".indexOf("Ipsum") Which is the main purpose of this function. Also with your invariant types are different. Not "abc".indexOf("b") but "abc".indexOf('b').