|
| 1 | +### LastIndexOf has improved handling of empty search strings |
| 2 | + |
| 3 | +<xref:System.String.LastIndexOf%2A?displayProperty=nameWithType> and related APIs now return correct values when searching for a zero-length (or zero-length equivalent) substring within a larger string. |
| 4 | + |
| 5 | +#### Change description |
| 6 | + |
| 7 | +In .NET Framework and .NET Core 1.0 - 3.1, <xref:System.String.LastIndexOf%2A?displayProperty=nameWithType> and related APIs might return an incorrect value when the caller searches for a zero-length substring. |
| 8 | + |
| 9 | +```csharp |
| 10 | +Console.WriteLine("Hello".LastIndexOf("")); // prints '4' (incorrect) |
| 11 | +
|
| 12 | +ReadOnlySpan<char> span = "Hello"; |
| 13 | +Console.WriteLine(span.LastIndexOf("")); // prints '0' (incorrect) |
| 14 | +``` |
| 15 | + |
| 16 | +Starting with .NET 5.0, these APIs return the correct value for `LastIndexOf`. |
| 17 | + |
| 18 | +```csharp |
| 19 | +Console.WriteLine("Hello".LastIndexOf("")); // prints '5' (correct) |
| 20 | +
|
| 21 | +ReadOnlySpan<char> span = "Hello"; |
| 22 | +Console.WriteLine(span.LastIndexOf("")); // prints '5' (correct) |
| 23 | +``` |
| 24 | + |
| 25 | +In these examples, `5` is the correct answer because `"Hello".Substring(5)` and `"Hello".AsSpan().Slice(5)` both produce an empty string, which is trivially equal to the empty substring that is sought. |
| 26 | + |
| 27 | +#### Reason for change |
| 28 | + |
| 29 | +This change was part of an overall bug fixing effort around string handling for .NET 5. It also helps unify our behavior between Windows and non-Windows platforms. For more information, see [dotnet/runtime#13383](https://github.com/dotnet/runtime/issues/13383) and [dotnet/runtime##13382](https://github.com/dotnet/runtime/issues/13382). |
| 30 | + |
| 31 | +#### Version introduced |
| 32 | + |
| 33 | +5.0 |
| 34 | + |
| 35 | +#### Recommended action |
| 36 | + |
| 37 | +You don't need to take any action. The .NET 5.0 runtime provides the new behaviors automatically. |
| 38 | + |
| 39 | +There is no compatibility switch to restore the old behavior. |
| 40 | + |
| 41 | +#### Category |
| 42 | + |
| 43 | +Core .NET libraries |
| 44 | + |
| 45 | +#### Affected APIs |
| 46 | + |
| 47 | +- <xref:System.String.LastIndexOf%2A?displayProperty=fullName> |
| 48 | +- <xref:System.Globalization.CompareInfo.LastIndexOf%2A?displayProperty=fullName> |
| 49 | +- <xref:System.MemoryExtensions.LastIndexOf%2A?displayProperty=fullName> |
| 50 | + |
| 51 | +<!-- |
| 52 | +
|
| 53 | +#### Affected APIs |
| 54 | +
|
| 55 | +- `Overload:System.String.LastIndexOf` |
| 56 | +- `Overload:System.Globalization.CompareInfo.LastIndexOf` |
| 57 | +- `Overload:System.MemoryExtensions.LastIndexOf` |
| 58 | +
|
| 59 | +--> |
0 commit comments