dotnet-api-docs
dotnet-api-docs copied to clipboard
string.LastIndexOf - State whether startIndex param is inclusive
The documentation for the String.LastIndexOf method needs to explicitly state whether the startIndex parameter is inclusive.
This is relevant for looping. If I call LastIndexOf repeatedly in a loop and set index to the position found, do I have to pass index-1 on subsequent calls or just index?
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.
Starting indexes are always considered inclusive. The documentation is also clear on this: it starts searching at the specified index:
The search starts at a specified character position and proceeds backward toward the beginning of the string for the specified number of character positions.
In other words, if you pass 5 for startIndex, it will start at value[5].
As @colejohnson66 wrote, the official docs already mention that: https://learn.microsoft.com/en-us/dotnet/api/system.string.lastindexof?view=net-8.0
@Sildoreth which docs did you mean?
The text that @colejohnson66 quoted is the very text that I finding lacking. Unless "Starting indexes are always considered inclusive" is explicitly documented somewhere, then it's not a given.
When would a starting index not be inclusive? If I do str[5..7], I would expect it starts the slice at index five.