dotnet / dotnet-api-docs

.NET API reference documentation (.NET 5+, .NET Core, .NET Framework)
https://docs.microsoft.com/dotnet/api/
Other
725 stars 1.56k forks source link

String.LastIndexOf(needle, startIndex) behavior would benefit from some clarification #7751

Open CaiusJard opened 2 years ago

CaiusJard commented 2 years ago

If we call"abcabcabc".LastIndexOf("abc", 4) we might reasonably expect it to return 3, having assessed the character at start index 4 to be 'b' and thus not equal to 'a' of the needle, and moved one position towards the start, where we can find 'a', 'b' and 'c' in indexes 3, 4 and 5 respectively

LastIndexOf actually returns 0, indicating the first match found is the "abc" at the start of the string. A comment in the source code explains the behavior, namely that LastIndexOf() overloads taking a startIndex do not consider characters to the right of the character at startIndex (so for a startIndex of 4, the trailing characters "cabc" are not searched/do not contribute towards finding a match; the search is executed as if the haystack is a Substring terminating after the startIndex character I.e. "abcab")

The current wording of the documentation only states where searching begins and states the direction but I believe it would benefit from an addition that explains the scope of the search doesn't extend to looking for characters to the right of startIndex

dotnet-issue-labeler[bot] commented 2 years ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost commented 2 years ago

Tagging subscribers to this area: @dotnet/area-system-globalization See info in area-owners.md if you want to be subscribed.

Issue Details
If we call`"abcabcabc".LastIndexOf("abc", 4)` we might reasonably expect it to return 3, having assessed the character at start index 4 to be 'b' and thus not equal to 'a' of the needle, and moved one position towards the start, where we can find 'a', 'b' and 'c' in indexes 3, 4 and 5 respectively LastIndexOf actually returns 0, indicating the first match found is the "abc" at the start of the string. A comment in the source code explains the behavior, namely that LastIndexOf() overloads taking a startIndex do not consider characters to the right of the character at startIndex (so for a startIndex of 4, the trailing characters "cabc" are not searched/do not contribute towards finding a match; the search is executed as if the haystack is a Substring terminating after the startIndex character I.e. "abcab") The current wording of the documentation only states where searching begins and states the direction but I believe it would benefit from an addition that explains the scope of the search doesn't extend to looking for characters to the right of startIndex
Author: CaiusJard
Assignees: -
Labels: `Pri3`, `area-System.Globalization`
Milestone: -
tarekgh commented 2 years ago

@CaiusJard are you interested to submit a PR to fix this? you may tag me in that PR. Thanks!