Skip to content

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

Closed

Description

@GrabYourPitchforks

(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.

Activity

  1. transferred this issue fromdotnet/coreclron Jan 31, 2020
  2. added this to the 5.0 milestone on Jan 31, 2020
  3. added
    untriaged
    on Feb 26, 2020
  4. removed
    untriaged
    on Apr 7, 2020
  5. ghost locked as resolved and limited conversation to collaborators on Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions