2 ms·
>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.
by hermitcrab 3y ago
>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.
Marinated on this a bit more. I think this the most straightforward and internally consistent way to think about this. Thanks for the insight.
So I figure based on this:
1-based first index of v1 in v2 is:
| v1 | v2 | IndexOf(v1,v2) |
|-------|-------------|----------------|
| [] | [] | 1 |
| aba | [] | N/A |
| [] | aba | 1 |
| a | a | 1 |
| a | aba | 1 |
| x | y | N/A |
| world | hello world | 7 |
Where []=empty.
This is the same as Excel FIND() and differs from Javascript indexOf() (ignoring difference in indexing) only for "".indexOf("") which returns -1 (N/A).
1-based last index of v1 in v2 is:
| v1 | v2 | LastIndexOf(v1,v2) |
|-------|-------------|--------------------|
| [] | [] | 1 |
| aba | [] | N/A |
| [] | aba | 4 |
| a | a | 1 |
| a | aba | 3 |
| x | y | N/A |
| world | hello world | 7 |
This differs from Javascript lastIndexOf() (ignoring difference in indexing) only for "".indexOf("") which returns -1 (N/A).
- jmholla 3y agoIf you add four spaces at the beginning of a line, Hacker News will treat it as preformatted. Here are you tables like such just for ease of reading. --- 1-based first index of v1 in v2 is: | v1 | v2 | IndexOf(v1,v2) | |-------|-------------|----------------| | [] | [] | 1 | | aba | [] | N/A | | [] | aba | 1 | | a | a | 1 | | a | aba | 1 | | x | y | N/A | | world | hello world | 7 | 1-based last index of v1 in v2 is: | v1 | v2 | LastIndexOf(v1,v2) | |-------|-------------|--------------------| | [] | [] | 1 | | aba | [] | N/A | | [] | aba | 4 | | a | a | 1 | | a | aba | 3 | | x | y | N/A | | world | hello world | 7 |
- hermitcrab 3y agoDidn't know that. Thanks!