3 ms·
>To further illustrate, it would be the index where, if the search string is removed from the found position, it would have to be inserted in order to revert to
by hermitcrab 3y ago
>To further illustrate, it would be the index where, if the search string is removed from the found position, it would have to be inserted in order to revert to the original string.
I guess that is a good way to look at it. It would mean:
indexof "" in "abca" is 0
indexof "abca" in "" is invalid
indexof "" in "" is 0
Which feels unintuitive.
- layer8 3y agoIf you feel that ‘indexof "" in "" is 0’ is unintuitive, consider that indexOf s in s is zero for all strings s (including for the empty string). Similarly, indexOf st in s is invalid for all strings s if t is non-empty, that is if s is a proper prefix of the search string. Your example ‘indexof "abca" in "" is invalid’ is just one case of that (with s = empty string and t = "abca"), completely analogous to ‘indexof "babca" in "b" is invalid’. So I’d say your intuition needs adjusting. ;)
- hermitcrab 3y agoMaybe. ;0) Your approach seems to be consistent. Unfortunately I think it is too complicated to explain to my non-programmer users.