Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string.LastIndexOf(string.Empty, ...) returns wrong index #13383

Closed
GrabYourPitchforks opened this issue Sep 10, 2019 · 0 comments · Fixed by #34616
Closed

string.LastIndexOf(string.Empty, ...) returns wrong index #13383

GrabYourPitchforks opened this issue Sep 10, 2019 · 0 comments · Fixed by #34616

Comments

@GrabYourPitchforks
Copy link
Member

(Related to https://github.com/dotnet/coreclr/issues/26608.)

The definition of string.IndexOf(string value, StringComparison comparisonType) is such that it returns the minimum integer index for which the following expression is true:

originalString.Substring(index).StartsWith(value, comparisonType) == true

Similarly, the definition of string.LastIndexOf(string value, StringComparison comparisonType) is such that it returns the maximum integer index for which the following expression is true:

originalString.Substring(index).StartsWith(value, comparisonType) == true

(If there is no value index which makes this expression true, IndexOf and LastIndexOf return -1.)

Given this definition, the result of someInputString.LastIndexOf("", StringComparison.Ordinal) should be someInputString.Length for all possible input strings. This is because calling someInputString.Substring(someInputString.Length) will result in the empty string, and any string (including the empty string) trivially starts with itself. Furthermore, there is no larger value index which can be passed into the Substring routine, as any such value would result in an exception.

@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@msftgits msftgits added this to the 5.0 milestone Jan 31, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 26, 2020
@adamsitnik adamsitnik removed the untriaged New issue has not been triaged by the area owner label Apr 7, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants