Description
The String API documentation for substring states
The substring of this string from start, inclusive, to end, exclusive.
It is reasonable to interpret this as start
is a valid index because it is inclusive and no relation to end
or length
is specified at this point. Then the end of the documentation states
Both start and end must be non-negative and no greater than length;
Which implies start==length
is valid. This can be confusing due to the earlier statement. Furthermore, it is not intuitive that . In my personal experience, I did not understand this nuance until I was testing code.start==end==length
is a valid case but a similar/symmetrical case start==-1
and end==0
is not valid. Both are empty ranges adjacent to the range of valid indices
I think the documentation could benefit from clarifying this. Perhaps starting with the statement 0 ≤ start ≤ end ≤ length must hold.
would clearly and quickly communicate the valid possible inputs. This change could also be applied to similar API e.g. List.sublist
.