dotnet-api-docs icon indicating copy to clipboard operation
dotnet-api-docs copied to clipboard

string.LastIndexOf - State whether startIndex param is inclusive

Open Sildoreth opened this issue 1 year ago • 4 comments

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?

Sildoreth avatar Apr 17 '24 16:04 Sildoreth

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

colejohnson66 avatar Apr 18 '24 12:04 colejohnson66

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?

adamsitnik avatar Apr 19 '24 15:04 adamsitnik

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.

Sildoreth avatar Apr 24 '24 16:04 Sildoreth

When would a starting index not be inclusive? If I do str[5..7], I would expect it starts the slice at index five.

colejohnson66 avatar May 29 '24 12:05 colejohnson66