I've seen most languages have it be the case for their substring method that using the length of a given string in the method as the start index will give you an empty string. It is most definitely helpful when writing algorithms involving successive shortening of the string down to an empty string. The problem I have is that it makes most of the summaries of these methods/functions look inaccurate when they describe the result.
For instance, Java's substring for String states that "The substring begins at the specified beginIndex and extends to the character at index endIndex - 1.". This makes sense for all values 0 <= i < len(string). As soon as you use len(string) however, what does that index refer to? In a language like C using C strings, it naturally becomes the null terminator which we treat as an empty string. The implementations I've seen specifically check the range of the indices to be [0, len(string)]. When only one argument is specified we will take the difference between the start index and the length of the string which happens to work out to 0 for startIndex=len(string).
I've come to believe this is just an unspoken convention among languages that go back to the roots of NULL terminated strings to act as empty strings. Can anyone shed some light beyond it's just the way it is?
str == substr(str, 0, i) + substr(str, i, length(str))
for all values ofi
.