3 ms·
The invariant is: haystack.substring(haystack.indexOf(needle), needle.length()) == needle And indexOf should return lowest such number. So it should ret
by ajuc 3y ago
The invariant is:
haystack.substring(haystack.indexOf(needle), needle.length()) == needle
And indexOf should return lowest such number.
So it should return 0. LastIndexOf too, because the haystack is empty.
For nonempty haystack it should be
"Abc".indexOf("") == 0
"Abc".lastIndexOf("") == 3
- l0b0 3y agoI'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').